Loading function...

Please make sure JavaScript is enabled. Otherwise you will be unable to see the function documentation.

Registers a new NativeScriptLoaded event handler, meaning Callback will be called when a base game script is loaded. The script's name and environment is passed to the function so it can apply changes and patches to the script, as well as give it modified versions of normal functions as a way of changing the script's behavior. Keep in mind that the name given is a full name, so you may need to print what it says when you expect a script to start in order to get the right name for it.

Arguments

Results

None.

Versions

DSL4 This function is now just for legacy support, and does the same thing calling RegisterLocalEventHandler with NativeScriptLoaded would.

Example

Change store prices to be free and stop the boy's dorm script from despawning little kids.

function F_ShopAddItem(...)
	arg[5] = 0
	return ShopAddItem(unpack(arg))
end
function F_Nothing()
end
AddScriptLoaderCallback(function(name, env)
	if name == "SStores.lua" then
		-- change SStores.lua's ShopAddItem to be our custom version so we can change prices
		env.ShopAddItem = F_ShopAddItem
	elseif name == "AreaScripts/Bdorm.lua" then
		-- change a function the bdorm script to stop little kids from being despawned
		env.F_KillAllLittleKids = F_Nothing
	end
	print("loaded "..name)
end)

Registers a new local event handler. The Callback function is called when the event is activated. The returned Event handler object is registered with the current script, meaning it will be removed when the script dies. If there is no current DSL script to register with, it will exist until removed manually with RemoveEventHandler.

An event Type is just a string. Although there are a few events built-in and listed below, you are free to define your own for any purpose. Just make sure the name is unique enough that it won't clash with what other developers are likely to use. This is a good way to allow your script's behavior to be influenced by scripts from other developers.

Arguments

Results

Example

Keep the player up past 2 AM.

RegisterLocalEventHandler("PlayerSleepCheck",function()
	return true
end)

Events (client)

Type Description
DSL6CameraFaded(ms, fadein) The camera is fading in or out. Same arguments as CameraFade, but the 2nd value is a boolean instead of a number.
DSL5ControllersUpdated() All controllers have been updated. This is a good time to call SetStickValue.
DSL5ControllerUpdating(id) A specific controller is being updated. This is a good time to call SetButtonPressed.
DSL5GameBeingPaused() The game will pause unless an event handler returns true.
DSL4NativeScriptLoaded(name, env) A base game script has been loaded and run, but not yet updated (meaning its main function hasn't run).
DSL5PedBeingCreated() A ped is about to be created. You can count peds with AllPeds to determine if you should delete a ped to prevent a crash.
DSL5PedStatOverriding(ped, stat, value) A ped's stat is being changed unless an event handler returns true. Beware setting the stat yourself will still activate the event.
DSL7PedUpdateThrottle(ped) A ped is updating their action throttle, which can happen multiple times per frame. This is a good time to call PedSetThrottle.
DSL7PlayerBuilding(is_player,is_cutscene) A ped's model will rebuild unless an event handler returns true. This can prevent replacing ped models with player clothes.
DSL5PlayerSleepCheck() The game wants to check if the player should fall asleep, but will skip the check if an event handler returns true.
DSL5SaveGameLoading() A save game is going to be loaded unless an event handler returns true.
DSL9ScriptDestroyed(script) A script object has been destroyed.
DSL9ScriptShutdown(script) A script object is being shutdown. This happens before destruction.
DSL7ServerConnected() The active server is now fully connected to.
DSL7ServerConnecting(address, hashes) A server connection has begun, and can be considered "active" until the ServerDisconnected event.
DSL7ServerDisconnected(address) A server has been disconnected from. Always comes after a ServerConnecting event (unless the game is quit).
DSL7ServerDownloading() The active server is downloading files for the first time.
DSL7ServerKicked(reason) The active server issued a kick (and a seperate ServerDisconnected event will still follow).
DSL7ServerListing(address, listing) A server has provided listing information. The listing table contains name, info, mode, icon, players, and max_players.
DSL5WindowGoingFullscreen() The game will enter exclusive fullscreen mode unless an event handler returns true.
DSL5WindowMinimizing() The game's window will be minimized unless an event handler returns true.
DSL5WindowUpdating(window) The game's window can be customly styled if any event handler returns true. The window table contains fields describing the window.

Events (server)

Type Description
DSL5PlayerConnected(player) A player has fully connected and can be used safely with any networking functions.
DSL5PlayerConnecting(player) A player is connecting and can only be used with some functions. Kick them to prevent them from being connected.
DSL5PlayerDropped(player) A player has been dropped and all references to them should be removed. Will always come after a PlayerConnecting event.
DSL5PlayerListing(player, listing) A player requested listing information for the server. You can modify the listing table's info and mode. The player is only valid for the duration of this event.
DSL8ServerShutdown() The server is being shutdown. The Lua state closes immediately after this event. This event is only triggered when the server is "gracefully" shutdown.

Events (shared)

Type Description
DSL5ScriptPrinted(text, type) One of the print functions have been called. This is mainly designed to help forward console output into a server chat.

Registers a new network event handler. The Callback function is called when the event is activated. The returned Event handler object is registered with the current script, meaning it will be removed when the script dies. If there is no current DSL script to register with, it will exist until removed manually with RemoveEventHandler.

An event Type is just a string. Unlike RegisterLocalEventHandler, there are no built-in events. Network events can only be activated by the SendNetworkEvent function being called on the other side of the connection.

Since the event is sent over a network connection, you should not trust the arguments given to your callback. Make sure to type check them if you do not want your script misbehaving.

On the server, the first argument given to your callback function is the Player id for the player that sent the event. You can always trust this is a valid player id.

Arguments

Results

Example

Tell all players in the server when a player dies.

client script

function main()
	while not SystemIsReady() do
		Wait(0)
	end
	while true do
		if PedIsDead(gPlayer) then
			SendNetworkEvent("example:playerDied") -- putting "yourModName:" in front of events helps prevent naming conflicts with other scripts
			repeat
				Wait(0)
			until not PedIsDead(gPlayer) -- wait until we're not dead so that the server isn't just spammed with events every frame we're dead
		end
		Wait(0)
	end
end
RegisterNetworkEventHandler("example:notifyPlayerDeath",function(name)
	TextPrintString(name.." has died!",3,2)
end)

server script

RegisterNetworkEventHandler("example:playerDied",function(player) -- on the server, a valid player is always the first argument
	SendNetworkEvent(-1,"example:notifyPlayerDeath",GetPlayerName(player)) -- using -1 as the first argument means this event is sent to ALL players
end)

Remove an event handler created with RegisterLocalEventHandler.

Arguments

Results

None.

Activate an event of Type, passing any Arguments you want. Keep in mind that tables in Lua are passed by reference, so a decent strategy for allowing other scripts to respond to your event is by passing some sort of table. The Result is false unless at least one of the callbacks return true. This can be used as a way of letting certain events be cancelled, or for any other arbitrary use.

Arguments

Results