Event not safe for net

I’m working on a personal resource right now and it seems that if I make multiple event handlers for the same event it prints out in the log that the event was not net safe. This is a very unclear message to me and I don’t really know how to solve it (except just deleting one of the event handlers). It also seems to happen both on client and server side events.

Could someone help me out this this because I’m just curious on the fix if any.

3 Likes

Code?
(20 characters)

It’s in DispatchSystem so:

-- sv_messages.lua
function onMsg(type, err, _args, calArgs)
    local source = _args[1]

    -- copying table so that event doesn't pick it up
    local args = deepcopy(_args)
    table.remove(args, 1)

    if calArgs[1] == 'silent' then return end
    
    -- then just a bunch of if else statements that don't really matter
end

AddEventHandler('dispatchsystem:event', onMsg)
-- sv_permissions.lua

function HasCivPermission(player)
	-- checking for the convar
	local convar = GetConvar('dsperm.civ', 'everyone')
	
	if convar == 'none' then
		return false
	elseif convar == 'everyone' then
		return true
	elseif conver == 'specific' then
		return IsPlayerAceAllowed(source, 'ds.civ')
	end
end

function HasLeoPermission(player)
	-- checking for the convar
	local convar = GetConvar('dsperm.leo', 'everyone')
	
	if convar == 'none' then
		return false
	elseif convar == 'everyone' then
		return true
	elseif conver == 'specific' then
		return IsPlayerAceAllowed(source, 'ds.leo')
	end
end

function GetDispatchPermissions()
	local type = GetConvar('dsperm.dispatch', 'everyone')
	local perms = {}
	
	local index = 0
	while true do
		local current = GetConvar('perm_dispatch'..index, 'invalid')
		if current == 'invalid' then
			break
		end
		table.insert(perms, current)
		
		index = index + 1
	end
	
	return type, perms
end

AddEventHandler('dispatchsystem:event', function(type, err, args, calArgs)
    if type == 'set_dispatch_perms' then
        for _, val in ipairs(args) do
            Citizen.Trace(val..'\n')
        end
    end
end)

Citizen.CreateThread(function()
	Wait(1500)
	local type, perms = GetDispatchPermissions()
	if string.lower(type) ~= 'specific' then
		table.insert(perms, type)
	end
	TriggerEvent('dispatchsystem:post', 'set_dispatch_perms', perms, {})
end)

When adding event handlers you need to register them.

For server-side script:

https://wiki.fivem.net/wiki/RegisterServerEvent

For client-side script:

https://wiki.fivem.net/wiki/RegisterNetEvent

1 Like

It’s already registered, if it wasn’t registered then it the event wouldn’t work at all right? I’m just getting the error message that it’s not net safe.

The error message “not net safe” comes from an event name not being registered as being able to be called see:

AddEventHandler('myEvent', function() end)

(Gives Error)

RegisterServerEvent('myEvent')
AddEventHandler('myEvent', function() end)

(Works fine)

1 Like

Try RegisterServerEvent ? If you have already tried RegisterNetEvent.

Yes it wouldn’t work, but you would see the event <eventname> was not safe for net message, that is if you are triggering a server event from the client or a client event from the server.

If you are triggering a server event from a server script (or client event from a client script) you don’t need to register the event (you can, but it works fine if you do it without registering).

For example:
server_script_one.lua

AddEventHandler('test', function()
    print("success")
end)

server_script_two.lua (note this is delayed to make sure the other script is loaded before executing the event)

Citizen.CreateThread(function()
    Citizen.Wait(100)
    TriggerEvent('test')
end)

Server console output:

success

However if you try to trigger the ‘test’ event from a client script, like so:

TriggerServerEvent('test')

You get this error in the client console.

event test was not safe for net

This is because the event isn’t registered to be used cross client/server.

To fix this, simply add RegisterServerEvent("test") to your server script like so:

RegisterServerEvent("test")
AddEventHandler('test', function()
    print("success")
end)

now the output will be this in your server console

success
success

(remember, it prints “success” twice because the server_script_two.lua triggers it once, then the client also triggers it.)


The example above just covers the server to server and client to server scenario, but it is the same for client to client and server to client scenarios.

This is also the same if you want to do it cross resource. example:
resource_one_server_script.lua

TriggerEvent('test")

resource_two_server_script.lua

AddEventHandler('test', function()
    print("success")
end)

Output in server console: success

10 Likes

Ok, the only thing is I’m getting this message on Server to Server events. I’ll go ahead and mark it as solve anyway because it’s a good explanation and I feel like I just darn goofed on something.

1 Like

Which resource manifest version are you using?

44febabe-d386-4d18-afbe-5e627f4af937 of course (the newest one)

Then I have no idea what the issue could be… Unless you somehow messed up and made one of the scripts a client script I honestly don’t know why it doesn’t work.

How my script works is the following (a lot of event stuff): When an NUI button is pressed, is has the client callback do some logic then trigger an event containing some information to another resource which is server sided. The server sided resource then triggers another event containing information calculated by the resource and some other information to another server event. The server event that everything ends up on is dispatchsystem:event.

1 Like

How do I fix “event skinchanger:loadDefaultModel was not safe for net” ?

Hey you! MAKE SURE THE REGISTERED EVENTS AND THE EVENT HANDLER EVENT’S NAME MATCHES 100%! Learning about lua, I ran into this error, and (because of the simplicity of my code), I’ve learned that it usually means a mismatched event name.

This is a security error to make sure rando code isn’t just executed!

bro i love you