Fist Posted September 9, 2015 Share Posted September 9, 2015 Hello! I have problem with getting local variable from server event. my code is like this: Thanks. ^^ CLIENT: triggerServerEvent( "getServerInfo", root, localPlayer, serverName) GUIEditor.label[1] = guiCreateLabel(13, 2, 521, 15, serverName, false, GUIEditor.gridlist[1]) SERVER: function getServerInfo(serverName) local serverName = getServerName() end addEvent( "getServerInfo", true ) addEventHandler( "getServerInfo", root, getServerInfo ) Link to comment
Mr_Moose Posted September 9, 2015 Share Posted September 9, 2015 You're doing it the wrong way, this will send the variable serverName (value: nil) from client to server, then you'll create a new local variable serverside where you get the server name. Take a look at: triggerClientEvent Link to comment
Fist Posted September 9, 2015 Author Share Posted September 9, 2015 You're doing it the wrong way, this will send the variable serverName (value: nil) from client to server, then you'll create a new local variable serverside where you get the server name. Take a look at: triggerClientEvent getServerName() is only server function. Link to comment
HUNGRY:3 Posted September 9, 2015 Share Posted September 9, 2015 (edited) Client addEvent("getClientServerName",true) function mhm(serverName) Lab = guiCreateLabel(13, 2, 521, 15, serverName, false) end addEventHandler("getClientServerName",root,mhm) server function getServerInfo() local serverName = getServerName() triggerClientEvent("getClientServerName",root,serverName) end addEventHandler( "onResourceStart", resourceRoot, getServerInfo ) not tested but should work Edited September 9, 2015 by Guest Link to comment
JR10 Posted September 9, 2015 Share Posted September 9, 2015 onResourceStart will be called for all resources, should be resourceRoot not root. And this will only trigger when the resource starts, what about when a player joins? Link to comment
Fist Posted September 9, 2015 Author Share Posted September 9, 2015 Client addEvent("getClientServerName",true) function mhm(serverName) Lab = guiCreateLabel(13, 2, 521, 15, serverName, false) end addEventHandler("getClientServerName",root,mhm) server function getServerInfo() local serverName = getServerName() triggerClientEvent("getClientServerName",root,serverName) end addEventHandler( "onResourceStart", resourceRoot, getServerInfo ) not tested but should work still nothing code: client: function recieveServerInfo(serverName) GUIEditor.label[1] = guiCreateLabel(13, 2, 521, 15, serverName, false, GUIEditor.gridlist[1]) end addEvent("recieveServerInfo", true) addEventHandler( "recieveServerInfo", root, recieveServerInfo ) server: function getServerInfo() local serverName = getServerName() triggerClientEvent("recieveServerInfo", root, serverName) end addEventHandler( "onResourceStart", resourceRoot, getServerInfo ) Link to comment
HUNGRY:3 Posted September 9, 2015 Share Posted September 9, 2015 Debugscript I'm sure it will say some thing Link to comment
Moderators Citizen Posted September 9, 2015 Moderators Share Posted September 9, 2015 You didn't understand how server and client sided scripts works and how they should send datas between eachothers. You probably didn't read the wiki in which everything is explained: https://wiki.multitheftauto.com/wiki/Sc ... de_scripts But basically, the server scripts are loaded and executed on the server's computer whereas client scripts are dowloaded from the server's computer when the player joins, loaded and then executed on the player's computer So as they are not running on the same computer, if you create a variable on the server side, this variable won't exist on the client side (and same thing the other way). /?\ But how to send datas from one side to another then ??? /?\ - You need to send the data from one side and store that data on the other side. This is where triggerServerEventand triggerClientEventcome in ! These two functions can be used to send data(s) from the client side to the server side and from the server side to the client side respectively. Here is how you should use it in your case: CLIENT: function createGuiStuff() -- gui grid list blablabla GUIEditor.label[1] = guiCreateLabel(13, 2, 521, 15, serverName, false, GUIEditor.gridlist[1]) -- Empty for now, it will be updated by our triggerXEvents -- rest of the gui blablabla -- and do this at the end: triggerServerEvent("getServerInfo", localPlayer) -- Ask the server to send data(s) about the server to us end addEventHandler("onClientResourceStart", resourceRoot, createGuiStuff) addEvent("getServerInfoCallback", true) -- We create an event that the server will use to send the datas to us function getServerInfoCallback( servName ) -- servName will contains what the server send to us guiSetText(GUIEditor.label[1], serverName) -- updating the variable serverName we created at the top end addEventHandler("getServerInfoCallback", root, getServerInfoCallback) SERVER: addEvent( "getServerInfo", true ) function getServerInfo() local playerAsking = source -- source is a predefined variable only when the function is called by an event local serverName = getServerName() triggerClientEvent(playerAsking, "getServerInfoCallback", playerAsking, serverName) -- The above function send serverName (4th arg) only for the playerAsking(1st arg) -- using the "getServerInfoCallback" event name (2nd arg) -- and setting playerAsking as the source of the event (3rd arg) end addEventHandler("getServerInfo", root, getServerInfo) Link to comment
Fist Posted September 9, 2015 Author Share Posted September 9, 2015 @Citizen Thank you so much. <3 I really appreciate u explained me everyting about trigger events. Link to comment
SpecT Posted September 10, 2015 Share Posted September 10, 2015 Lol why don't you use "return" instead of callback function ? I know that there is already solution but this is an easier and less code version. CLIENT: function createGuiStuff() -- gui grid list blablabla GUIEditor.label[1] = guiCreateLabel(13, 2, 521, 15, serverName, false, GUIEditor.gridlist[1]) -- Empty for now, it will be updated by our triggerXEvents -- rest of the gui blablabla -- and do this at the end: local serverName = triggerServerEvent("getServerInfo", localPlayer) -- Ask the server to send data(s) about the server to us end addEventHandler("onClientResourceStart", resourceRoot, createGuiStuff) SERVER: function getServerInfo() local playerAsking = source local serverName = getServerName() return serverName end addEvent("getServerInfo", true) addEventHandler("getServerInfo", root, getServerInfo) Link to comment
JR10 Posted September 10, 2015 Share Posted September 10, 2015 Lol why don't you use "return" instead of callback function ?I know that there is already solution but this is an easier and less code version. Because it doesn't work like that? You can't cross-return data. You have to trigger another event to return any data. Link to comment
Moderators Citizen Posted September 10, 2015 Moderators Share Posted September 10, 2015 Lol why don't you use "return" instead of callback function ?I know that there is already solution but this is an easier and less code version. CLIENT: function createGuiStuff() -- gui grid list blablabla GUIEditor.label[1] = guiCreateLabel(13, 2, 521, 15, serverName, false, GUIEditor.gridlist[1]) -- Empty for now, it will be updated by our triggerXEvents -- rest of the gui blablabla -- and do this at the end: local serverName = triggerServerEvent("getServerInfo", localPlayer) -- Ask the server to send data(s) about the server to us end addEventHandler("onClientResourceStart", resourceRoot, createGuiStuff) SERVER: function getServerInfo() local playerAsking = source local serverName = getServerName() return serverName end addEvent("getServerInfo", true) addEventHandler("getServerInfo", root, getServerInfo) Lol Link to comment
SpecT Posted September 11, 2015 Share Posted September 11, 2015 Lol why don't you use "return" instead of callback function ?I know that there is already solution but this is an easier and less code version. Because it doesn't work like that? You can't cross-return data. You have to trigger another event to return any data. Oh yeah ... I'm sorry, my bad 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