Lergen Posted June 14, 2020 Share Posted June 14, 2020 Hello. I'm a bit stumped on something and can't quite figure out where I'm going wrong here. I want to pass a variable from the server to the client, but all my attempts return the variable come back as nil. Any advice on what I'm doing wrong here? Here's a simple example where I try to pass "randomPlayer" to output on a client's chatbox but returns the player element as nil. I've read the wiki and some other posts regarding this but I still don't quite get it. Server: addEvent ("getRandomPlayer",true) function randomPlayer () randomPlayer = getRandomPlayer() end addEventHandler ("getRandomPlayer", resourceRoot, randomPlayer) Client: function checkPlayer ( commandName ) triggerServerEvent ( "getRandomPlayer", resourceRoot, randomPlayer ) outputChatBox ( getPlayerName ( randomPlayer ).." is the player!" ) end addCommandHandler ( "check", checkPlayer ) Link to comment
Sisqo0 Posted June 14, 2020 Share Posted June 14, 2020 As i know triggerServerEvent pass information from client to server not to receive You can make all the code in serverside --Server function randomPlayer (playerSource, commandName) outputChatBox(getPlayerName(getRandomPlayer()).." is the player!",playerSource ) end addCommandHandler("check", randomPlayer) But if you want to get it on client side then you should use those both triggerServerEvent triggerClientEvent Link to comment
Lergen Posted June 14, 2020 Author Share Posted June 14, 2020 11 hours ago, Sisqo0 said: As i know triggerServerEvent pass information from client to server not to receive You can make all the code in serverside --Server function randomPlayer (playerSource, commandName) outputChatBox(getPlayerName(getRandomPlayer()).." is the player!",playerSource ) end addCommandHandler("check", randomPlayer) But if you want to get it on client side then you should use those both triggerServerEvent triggerClientEvent Thank you for your help, although I'm still a bit confused. So I need to trigger a client-side event from the server to actually pass the information? I tried doing something like this: --SERVER addEvent ("getRandomPlayer",true) function randomPlayer () randomPlayer = getRandomPlayer() triggerClientEvent (root, "checkPlayer", root, randomPlayer) end addEventHandler ("getRandomPlayer", resourceRoot, randomPlayer) --CLIENT addEvent("checkPlayer", true) function checkPlayer ( commandName ) triggerServerEvent ( "getRandomPlayer", resourceRoot, randomPlayer ) outputChatBox ( getPlayerName ( randomPlayer ).." is the player!" ) end addCommandHandler ( "check", checkPlayer ) addEventHandler("checkPlayer", root, checkPlayer) Although this still returns "player" as nil. I know technically you could do this all server-side, I'm just using it as an example to get info from a server-side variable to the client. Link to comment
Sisqo0 Posted June 14, 2020 Share Posted June 14, 2020 Try this:- --server function getplayer() triggerClientEvent(root,"randomPlayer",root,getRandomPlayer() ) end addEvent("getplayer", true) addEventHandler("getplayer" , resourceRoot, getplayer ) -- client function checkPlayer(commandName) triggerServerEvent("getplayer", resourceRoot) end addCommandHandler("check", checkPlayer) function randomPlayer (player) outputChatBox ( "The player is " .. player ) end addEvent("randomPlayer", true) addEventHandler("randomPlayer" , resourceRoot, randomPlayer) Or you can try this better --server function randomPlayer (playerSource, commandName ) triggerClientEvent(playerSource ,"checkplayer", playerSource, getRandomPlayer() ) end addCommandHandler("check", randomPlayer) --client function checkPlayer (player) outputChatBox("The player is "..player ) end addEvent("checkplayer", true) addEventHandler ("checkplayer", localPlayer, checkplayer) 1 Link to comment
Lergen Posted June 15, 2020 Author Share Posted June 15, 2020 Thank you! I've been playing around with it, and I think I have a pretty solid understanding of how it works now. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now