Hey guys! Learning Lua here…(How do you guys do object oriented, I’ll never know!)
Anyways, I’m having some trouble here and it’s in the server sided scripting. Looking at @daZepelin 's awesome tutorials, I seem to be stuck trying to get the AddEventHandler
Here’s the breakdown:
R1SL Registers and Adds Event Handler for my resource ‘hello_world:printToAll’
while R1CL Triggers server event ‘hello_world:printToAll’
Now, I was told that I could add event handlers for events across resources, SO, in my R2SL, I’ve simply just AddEventHandler for ‘hello_world:printToAll’ and placed my code within the block and terminated it with ‘end’
HOWEVER, when trying to execute, R1SL executes fine (the resource that contains the actual registration of the eventhandler and it’s own addserverevent, HOWEVER, R2SL (which only has addserverevent with no registration) does NOT work. It’s supposed to print to the server console, but I only get greeted with:
“cfx> event hello_world:printToAll was not safe for net”
Which tells me the eventname isn’t recognized across resources.
You need to register the net event in the second resource too.
“event X was not safe for net” will always mean that an event was called over the net (cl->sv or sv->cl) but that event is not registered.
If you want to use a net event in multiple scripts, you have to register it in all the resources* that you need it.
Ahhh, OKAY! So I don’t have to register server event, BUT I have to register net event…That’s still a bit convoluted to me. I get exactly what you’re saying, but the ‘why’ is still a bit cloudy for meh.
Maybe if I knew the difference between RegisterNetEvent and RegisterServerEvent, I’d be good!
I will mark your answer as a solution in the meantime, but if you know the difference, it’d help me tremendously!
RegisterNetEvent is required for every event that you want to have a server-client connection (so you can trigger server events from a client, or client events from the server).
RegisterServerEvent and RegisterClientEvent are only used to register serverside-only / clientside-only events. Those can only be triggered using TriggerEvent. In lua at least, you don’t even need to register server-only / client-only events, only net events.
That is partially true.
You can add event handlers in other resources, and they all get triggered on TriggerEvent (so client trigger - client handle, or server trigger - server handle) without any registration, BUT, from my personal testing, the “net event registration” is available only in the resource that you register it
This means that if you have, let’s say, 6 server-side event handlers in 6 different resources, and you want all of them triggered by a client script, you have to use RegisterNetEvent in all 6 resources.
It’s a bit hard for me to explain “why”…because I don’t really know, I mostly learned this through trial and error.
RegisterNetEvent and RegisterServerEvent are the same function, the server one is just an older name for it.
There’s no “RegisterClientEvent”, even.
RegisterNetEvent, meanwhile, marks handlers in the current resource as “safe” for remote use. It makes sense to be per-resource, since otherwise you could end up with unintended resource dependencies.
@nta That’s okay! Dependencies are actually what I’m looking forward to;
So regarding your last comment: “marks it as safe for remote use”
Are there any other requirements for triggering an event in a secondary resource from a primary client resource?
I feel like I’m missing something
(P.S. I can upload my source if you’re interested!)
Or maybe I just have the wrong idea of “remote use”
If it’s usable remotely, can I not just open a server resource and add an event handler for that name, or does EVERY resource you create require the registry of the event (regardless of if that event name exists in other resources)?
Sorry if I’m being annoying. I promise to help anyone who has a similar question in the forums!
@nta You there mah boi? I’m only asking because maybe I didn’t understand the context of what your saying compared to the scope of the tutorial I’m looking at. When you said,
" RegisterNetEvent, meanwhile, marks handlers in the current resource as “safe” for remote use"
Are there any additional requirements for remotely activating an event from a resource different than the one the RegisterNetEvent was activated in?