Jump to content

Passing a server variable to the client


Lergen

Recommended Posts

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

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
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

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) 

 

  • Thanks 1
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...