Yazir Posted January 30, 2017 Share Posted January 30, 2017 Hello! I have a problem. I can't get getElementData() to work locally. It is initiated by server and synced. -- here's the initiation if getElementData(player,"money") == false then setElementData(player,"money",100) end -- that's code from another resource local money = getElementData( localPlayer, "money") outputChatBox( tostring(money) ) -- return: false And as I said there. I set it at one resource then i want to retrieve it at other. Maybe it's the problem? Yes. I've read the wiki but there's not much on client usage. Link to comment
koragg Posted January 30, 2017 Share Posted January 30, 2017 (edited) -- here's the initiation setElementData(player,"money",100) -- that's code from another resource local money = getElementData( localPlayer, "money") if money then outputChatBox( tostring(money) ) end Try that^. When you don't have the element data "money" set it is not 'false', it's just not there so no need to check I think. Edited January 30, 2017 by koragg Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 I need to check because i don't want the money to be reset. This value is already set but client doesn't see it. Link to comment
marty000123 Posted January 30, 2017 Share Posted January 30, 2017 Why do you handle money with setElementData? Link to comment
Hoffmann Posted January 30, 2017 Share Posted January 30, 2017 (edited) If you want to make such thing on client side use tables and export them among the scripts because it's very useful and safely to use them if you don't need to synchronize this data with all scripts. usermoney = {} -- Create empty table to insert data Setting data for player example(an element is used as table's index in MTA): usermoney[player] = 500 -- Set player's money to 500 Edited January 30, 2017 by NeverUnbeatable Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 How do i synchronize it then? Link to comment
Hoffmann Posted January 30, 2017 Share Posted January 30, 2017 (edited) 1 hour ago, Yazir said: How do i synchronize it then? Use getElementData and setElementData or export tables. Edited January 30, 2017 by NeverUnbeatable Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 I don't really understand how that would change anything. And that misses the point of my question here. Element Data won't work and I don't know why. Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 I've already said i use getElementData and setElementData but it won't work @NeverUnbeatable I forgot to mention - init resource is server side while the second part of the code is client side. Link to comment
LoPollo Posted January 30, 2017 Share Posted January 30, 2017 9 hours ago, koragg said: When you don't have the element data "money" set it is not 'false', it's just not there so no need to check I think. Returns This function returns a variable containing the requested element data, or false if the element or the element data does not exist. When getting data corresponding to a XML attribute, this is always a string. When the data is not found, it DOES return false. @Yazir your script seems fine. So: Are you sure the init script is called before the client? Are you sure the "player" parameter is correct? Maybe it's for some reason invalid, and the data is not set. Also an error is thrown Did you copied-pasted the scripts? (just to make sure there are no syntax errors in the script) From point 2, are there errors/warnings thrown in general? Check both clients and server As is said the script seems fine to me, setElementData by default sync the data server<->client, and it depends on elements, shared between resources (so fetching the data from different resource is not the problem as you said). Try also adding (if you haven't done it yet) output inside the code blocks, so you "see" what's happening inside your scripts (this is just to check the "logic" part of the script in case point 4 is "there are no errors": in case the server block for some reason is skipped, the data will be false) That's all i can think right now 2 Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 (edited) I know what is the reason. I thought the ElementData stays on players even when they disconnect. It doesn't so I have another question. How can i save that data so it could be retrieved when the player connects? If you have any quick answers, please do. In the meantime I'll be looking around in some already done resources to see how it has been made. To answer your @LoPollo though: 1. Yes 2. Yes. 3. Yes, fragments. 4. There have been errors that this value is a boolean, but it is known why now. Edited January 30, 2017 by Yazir Link to comment
LoPollo Posted January 30, 2017 Share Posted January 30, 2017 (edited) You can save data in lots of ways: if the data is relevant only for the current session of the server/resource, you can save the data at root/resourceRoot (this data will be lost on server restart/resource restart) You can also save data with accounts. The player must be logged in, as guests accounts will have their data deleted on disconnect You can use databases, using serials to identify the players... and many others The best way is using accounts in my opinion Edited January 30, 2017 by LoPollo 1 Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 (edited) I've tried using accounts but client can't read it (i guess that's good) so I think I'll have to load it when player connects and save when he disconnects. If server is shutdown, does "onPlayerQuit" fire for every player on the server? Or I could save it when the money changes. Edited January 30, 2017 by Yazir Link to comment
LoPollo Posted January 30, 2017 Share Posted January 30, 2017 Accounts are only serverside, so you should get that data with events, or as you said syncing it on elements. onPlayerQuit is serverside, it triggers once per player quit on the server. onClientPlayerQuit triggers once per player quit for ALL players (in their client side scripts) Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 (edited) Maybe I didn't make it clear. When there are players on the server and I type shutdown in the console. Will onPlayerQuit still get triggered? Edited January 30, 2017 by Yazir Link to comment
Hoffmann Posted January 30, 2017 Share Posted January 30, 2017 2 minutes ago, Yazir said: Maybe I didn't make it clear. When there are players on the server and I type shutdown in the console. Will onPlayerQuit still get triggered? Element data is deleted when player quit. Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 Will event "onPlayerQuit" be activated when server gets shut down (not when it crashes, of course it won't then). That's the third time I ask this question and I'll probably check it myself. Link to comment
LoPollo Posted January 30, 2017 Share Posted January 30, 2017 (edited) 8 minutes ago, Yazir said: Maybe I didn't make it clear. When there are players on the server and I type shutdown in the console. Will onPlayerQuit still get triggered? shutdown is not a command (if i didn't forget anything).. do you mean quit? when a client types quit, yes the server onPlayerQuit gets triggered. If you mean that you have a script that add the command shutdown to call the function Shutdown... then it's a good question, i don't know the answer honestly. But you can find it out testing PS: if it's a source of confusion, server events exist only for server-side scripts, and client events exist only for client-side script. EDIT: 3 minutes ago, Yazir said: Will event "onPlayerQuit" be activated when server gets shut down (not when it crashes, of course it won't then). That's the third time I ask this question and I'll probably check it myself. PS: in this case i'm sure onResourceStop gets called, just so you know Edited January 30, 2017 by LoPollo Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 I mean when I stop the server the resource is on. Just don't mind. I'll check it myself Link to comment
Yazir Posted January 30, 2017 Author Share Posted January 30, 2017 (edited) @LoPollo this is other question that you might know the answer. Instead of spamming I'll write it here. Why when I set scoreboard to this local players = getElementsByType( "player" ) function setScoreboard( ) call ( getResourceFromName( "scoreboard" ), "scoreboardResetColumns") call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "test", 70, "tetsefeas",17) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "murders", 70, "Player kills", 12) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "zombieskilled", 70, "Zombie kills",13) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "headshots", 70, "Headshots",15) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "daysalive", 70, "Days Alive",16) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "totalkills", 70, "Total Frags",14) call ( getResourceFromName( "scoreboard"), "scoreboardSetColumnPriority", "name", 11) for theKey,thePlayer in ipairs(players) do setElementData( thePlayer, "test", 20 ) outputChatBox( "3") end end addEventHandler( "onClientResourceStart", getRootElement( ), setScoreboard ) It shows me this in game: It worked before. The next day it went to this. @edit Oh I know what I'm doing wrong here. Let me check if it is the reason. @edit2 It won't work but I've updated the code anyway. Edited January 30, 2017 by Yazir Link to comment
LoPollo Posted January 31, 2017 Share Posted January 31, 2017 (edited) I'm not an expert on scoreboards, but after reading Resource:Dxscoreboard i think i know why it's happening: Quote bool scoreboardAddColumn ( string name, [ element forElement = getRootElement(), int width = 70, string friendlyName = name, int priority = slot after "name" column ] ) ^^^that's the syntax what's below are the parameters you pass. "test", 70, "tetsefeas",17 test is the name, 70 i think it should be the width BUT is the forElement, and since it's not an element the function scoreboardAddColumn fails. The fail may cause the problem. See if this works (i just inserted root as forElement): local players = getElementsByType( "player" ) function setScoreboard( ) call ( getResourceFromName( "scoreboard" ), "scoreboardResetColumns") call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "test", 70, "tetsefeas",17) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "murders", 70, "Player kills", 12) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "zombieskilled", 70, "Zombie kills",13) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "headshots", 70, "Headshots",15) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "daysalive", 70, "Days Alive",16) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", root, "totalkills", 70, "Total Frags",14) call ( getResourceFromName( "scoreboard"), "scoreboardSetColumnPriority", "name", 11) for theKey,thePlayer in ipairs(players) do setElementData( thePlayer, "test", 20 ) outputChatBox( "3") end end addEventHandler( "onClientResourceStart", getRootElement( ), setScoreboard ) 13 hours ago, Yazir said: Instead of spamming I'll write it here. Instead it's a good idea to create another post, it's another argument. Using this general rule, everyone will be able to find what's his looking for faster, if having a similar problem. Edited January 31, 2017 by LoPollo Link to comment
Yazir Posted January 31, 2017 Author Share Posted January 31, 2017 It's clientside script so the syntax is bool scoreboardAddColumn ( string name, [ int width = 70, string friendlyName = name, int priority = slot after "name" column, function textFunction = nil ] ) call ( getResourceFromName( "scoreboard" ), "scoreboardAddColumn", "test", 70, "tetsefeas",17) name, width, friendlyname and priority. Link to comment
LoPollo Posted January 31, 2017 Share Posted January 31, 2017 Didn't notice that, sorry. Are there errors clientside? Try also do some basic debug by adding some output between lines, checking also the returns of the calls. Try also temporarily commenting the call to the function scoreboardSetColumnPriority on name: int priority = slot after "name" column Link to comment
Yazir Posted February 2, 2017 Author Share Posted February 2, 2017 @LoPollo There are no errors, everything in the script is fired. I think I need to start a new topic about that issue. 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