Ziemo Posted April 24, 2019 Share Posted April 24, 2019 Hi, I have a little problem with my script. I wanted to use server variable in client-side script, so I used trigger server and client event, but unfortunately, it didn't work. Do you know why? PS: This script has to display variable characterName in grid row. SERVER SIDE SCRIPT: function getCharacterName(characterName) local characterName = 'James' triggerClientEvent ( thePlayer, 'getCharName', thePlayer, characterName ) end addEvent('getCharacterName', true) addEventHandler('getCharacterName', root, getCharacterName) CLIENT-SIDE SCRIPT: function openCharacterMenu(characterName) menu = guiCreateWindow(0.5, 0.5, 0.2, 0.25, 'Characters', true) charactersList = guiCreateGridList(0.5, 0.5, 0.4, 0.6, true, menu) charactersColumn = guiGridListAddColumn(charactersList, 'Characters', 0.6) charactersRow = guiGridListAddRow(charactersList, 'No characters found') guiGridListSetItemText(charactersList, charactersColumn, charactersRow, characterName, false, false) end addEvent('getCharName', true) addEventHandler('login.success', localPlayer, openCharacterMenu) I see one error in client debug: Bad argument @ 'gu'iGridListSetItemText [expected string at argument 4 got, nil] Event 'login.success' opens up characters gui Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 getCharName is a different event than login.success. Use getCharName on line 10, instead of login.success. Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 But it won't open up the whole gui. I want to open this gui after successful client login. Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 6 minutes ago, Ziemo said: But it won't open up the whole gui. I want to open this gui after successful client login. You cannot change two tasks in to one task, that is not logic, even for humans. Task 1. Send over the name. (Save in to a variable) Task 2. Open the GUI and use the variable. Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 21 minutes ago, IIYAMA said: You cannot change two tasks in to one task, that is not logic, even for humans. Task 1. Send over the name. (Save in to a variable) Task 2. Open the GUI and use the variable. So how can i make a function that calls server-side variable? Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 1 minute ago, Ziemo said: So how can i make a function that calls server-side variable? clientside variable. You must have already send it before you login. Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 1 hour ago, IIYAMA said: clientside variable. You must have already send it before you login. so client variable should look like this? characterName = triggerServerEvent('getCharacterName', localPlayer, characterName) Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 triggerServerEvent cannot send that data back like that. We are talking about two different systems that have a connection delay in between. (also known as PING) If you want that name clientside and clientside is the one that is requesting it. A clientside (request) > B serverside (getting the name) > clientside (receive) A triggerServerEvent > B triggerClientEvent Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 so this variable in client script should look like this? characterName = triggerServerEvent('getCharacterName', localPlayer, characterName) Link to comment
Scripting Moderators ds1-e Posted April 24, 2019 Scripting Moderators Share Posted April 24, 2019 (edited) 44 minutes ago, Ziemo said: so this variable in client script should look like this? characterName = triggerServerEvent('getCharacterName', localPlayer, characterName) triggerServerEvent: Returns true if the event trigger has been sent, false if invalid arguments were specified or a client side element was a parameter. I'll show you an example, to make things easier for you. It wasn't tested, but should work. -- clientside local first_variable = "Empty" local second_variable = "Nothing here" triggerServerEvent("onPlayerUseItem", getLocalPlayer(), first_variable, second_variable) -- serverside function onPlayerUseItem(first_arg, second_arg) local player = source -- event source outputChatBox(first_arg..", "..second_arg, player) end addEvent("onPlayerUseItem", true) addEventHandler("onPlayerUseItem", getRootElement(), onPlayerUseItem) This example shows how to get data from trigger, and if you want you can save it to variable. Edited April 24, 2019 by majqq 1 Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 2 hours ago, majqq said: triggerServerEvent: Returns true if the event trigger has been sent, false if invalid arguments were specified or a client side element was a parameter. I'll show you an example, to make things easier for you. It wasn't tested, but should work. -- clientside local first_variable = "Empty" local second_variable = "Nothing here" triggerServerEvent("onPlayerUseItem", getLocalPlayer(), first_variable, second_variable) -- serverside function onPlayerUseItem(first_arg, second_arg) local player = source -- event source outputChatBox(first_arg..", "..second_arg, player) end addEvent("onPlayerUseItem", true) addEventHandler("onPlayerUseItem", getRootElement(), onPlayerUseItem) This example shows how to get data from trigger, and if you want you can save it to variable. I think it will work, but I want to make the opposite thing, pass a variable from server to client. Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 I changed my code to this: SERVER: local characterName = 'John Doe' triggerClientEvent('showCharacters', root, characterName) CLIENT: function showCharacters(characterName) outputChatBox(characterName, localPlayer) end addEvent('showCharacters', true) addEventHandler('onResourceStart', localPlayer, showCharacters) But it isn't working. I have an error: Server triggered clientside event showCharacters, but event is not added clientside. Link to comment
Moderators IIYAMA Posted April 24, 2019 Moderators Share Posted April 24, 2019 (edited) 34 minutes ago, Ziemo said: I changed my code to this: SERVER: local characterName = 'John Doe' triggerClientEvent('showCharacters', root, characterName) CLIENT: function showCharacters(characterName) outputChatBox(characterName, localPlayer) end addEvent('showCharacters', true) addEventHandler('onResourceStart', localPlayer, showCharacters) But it isn't working. I have an error: Server triggered clientside event showCharacters, but event is not added clientside. Because the client hasn't loaded yet. Because the addEventHandler hasn't the right event. Should be "showCharacters" The base Element is incorrect. You sending the parent = root from serverside element to a handler with the child = localPlayer at clientside.root (container) > localPlayer (child) < NOT WORKINGlocalPlayer (child) > root (container) < WORKING (You are sending a triggerClientEvent, without target, so sending it to every player in game.) In case of sending it directly after loading the resource: (best practice) CLIENT addEventHandler("onClientResourceStart", resourceRoot, function () triggerServerEvent("onThisClientResourceStart", resourceRoot) end, false) SERVER local characterName = 'John Doe' addEvent("onThisClientResourceStart", true) addEventHandler("onThisClientResourceStart", resourceRoot, function () -- client is the target (the player that executed the event: onThisClientResourceStart) triggerClientEvent(client, 'showCharacters', resourceRoot, characterName) end, false) CLIENT function showCharacters(characterName) outputChatBox(characterName) -- localPlayer < this is running on the client/player his pc. No need to define the receiver, as only this player can receive the message end addEvent('showCharacters', true) addEventHandler('showCharacters', resourceRoot, showCharacters, false) Edited April 24, 2019 by IIYAMA 1 Link to comment
Ziemo Posted April 24, 2019 Author Share Posted April 24, 2019 It's working, thank you both so much! 2 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