Jump to content

addEventHandler How To Use Correctly


harryh

Recommended Posts

Hi, so I have never really used event handlers properly so if I trigger a client event using the element getLocalPlayer() what must I put in the server add event handler to return the same element. I put getRootElement() but I think thats wrong.

Code below is just an example of how I use addEventHandler

Client:

function check()

triggerServerEvent("stuff", getLocalPlayer())

end

addEventHandler("check", getLocalPlayer(), theFunction)



server:

addEventHandler("stuff", getRootElement(), stuffFunction) <------------ I would use this if calling in serverside because I dont know how to get the player. Normally I use source inside the function because that returns what executed the command or event

 

Thanks

Edited by harryh
Link to comment

Ok, I'll explain you a little trick I use :

When you trigger a server event by client use like this :

-- Server :
function helloWorld(source,message)
  outputChatBox(getPlayerName(source),source)
end
addEventHandler("helloWorld", root, helloWorld)

-- Client :

function helloWorld()
  message = "I like potatoes"
  triggerServerEvent("helloWorld", localPlayer, getLocalPlayer(), message) -- localPlayer and getLocalPlayer() will return the same USERDATA (the local player) but, like this, you can have source into server side
end
addCommandHandler("hello",helloWorld)

I hope you understand

Edited by Gordon_G
  • Like 2
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...