Drakath Posted October 28, 2014 Share Posted October 28, 2014 How can I trigger a server event via client side when a player leaves? Link to comment
Moderators IIYAMA Posted October 28, 2014 Moderators Share Posted October 28, 2014 I don't think that is possible and even if it is possible, there isn't guaranteed it will reach the server. Connection will be broken after unloading of the resources. Link to comment
Drakath Posted October 28, 2014 Author Share Posted October 28, 2014 So there is no way to save data via setAccountData that comes from client side script? I have some client timers that I need to save. Link to comment
-Blaze- Posted October 28, 2014 Share Posted October 28, 2014 well Drakath , even i wanted to save some stuff just like you. Apparently it is not possible and this kind of thing isn't discovered yet. Link to comment
Moderators IIYAMA Posted October 28, 2014 Moderators Share Posted October 28, 2014 @Drakath No there isn't, you have to sync the data like your player is sync his position to the server. So as you said a timer and triggerServerEvent. Depending how critical the data is, will require another update rate. How critical is the data and must it be secure? Because there is also clientside saving.(which can be edited by smart guys and will not be included at other pc's you play it on) Link to comment
Drakath Posted October 29, 2014 Author Share Posted October 29, 2014 Well, I wouldn't like anyone to change it. (If you have XML saving in mind). I just have a drugs system and I just want to save its effects duration. I saw some servers like SAUR and CIT do it. I guess they use server timers. Link to comment
Anubhav Posted October 29, 2014 Share Posted October 29, 2014 Store it in a client sided table then trigger it to server then remove the data from the table. Link to comment
Drakath Posted October 29, 2014 Author Share Posted October 29, 2014 I can do that easily but the question is how do you send that table to server when a player leaves the server. Link to comment
Anubhav Posted October 29, 2014 Share Posted October 29, 2014 triggerServerEvent("saveDataForPlayer", resourceRoot, table) Link to comment
.:HyPeX:. Posted October 30, 2014 Share Posted October 30, 2014 Well you could just save the data both client and server side, and when the player leaves freeze the server side. (Check the reason for living with onPlayerQuit - if he leaved manually, for this purpouse it is important if it was manual rather than dropping). If he reconnects back and the difference is much more than should be, then you can assume it was manually edited and you can punish/reset him. @Anubhav - Please read the thread correctly next time. EDIT: @OP: --onPlayerQuit string quitType, string reason, element responsibleElement quitType: How the player left. This argument can be: "Unknown" "Quit" "Kicked" "Banned" "Bad Connection" "Timed out" Link to comment
Saml1er Posted October 30, 2014 Share Posted October 30, 2014 You can solve this by updating that data server side ( each time you update it client side ) and save it onPlayerQuit or you can use setElementData. It also works onPlayerQuit. addEventHandler("onPlayerQuit",root, function () local data = getElementData (source,str) if not data then return end -- Do your stuff with data variable end) But it's better to save it server side. Believe me it's possible. If you're trying to save join time then do something like this: local t = {} addEventHandler("onPlayerJoin",root,function() t[source] = getTickCount() end ) addEventHandler("onPlayerQuit",root,function() local joinTime = t[source] - getTickCount() -- ms -- now do your stuff end ) Btw can you tell me what are you trying to save? Link to comment
Drakath Posted October 30, 2014 Author Share Posted October 30, 2014 I'm trying to save the remaining duration of some timers. I am working with getTickCount() as you suggested but I have a small problem: if time and client then timers[client][1] = time end ERROR: attempt to index field "?" (a nil value) Which one is nil? I checked that time and client is not nil. Link to comment
Moderators IIYAMA Posted October 30, 2014 Moderators Share Posted October 30, 2014 You don't have two tables. Remove that [1] Your index isn't 1, it is only the client. Link to comment
Drakath Posted October 30, 2014 Author Share Posted October 30, 2014 But I need to store 2 things with timers[client]. Do I need to create another table for that? Link to comment
Moderators IIYAMA Posted October 30, 2014 Moderators Share Posted October 30, 2014 of course you need another table. Every time you index a table, it is requires a table, as simple as that. if time and client then timers[client] = {time} -- time at pos [1] end Link to comment
Drakath Posted October 30, 2014 Author Share Posted October 30, 2014 What if the range of things that I need to store can differ from 1 to 8? I also need to keep that index. This is the script that I had: function q(data1, data2) if data2 == 1 then timers[client][1] = data1 timers[client][2] = getTickCount() end if data2 == 2 then timers[client][3] = data1 timers[client][4] = getTickCount() end if data2 == 3 then timers[client][5] = data1 timers[client][6] = getTickCount() end if data2 == 4 then timers[client][7] = data1 timers[client][8] = getTickCount() end end This can be 1 and 2 or 3 and 4 or 5 and 6 or 7 and 8. How can I store them while keeping the right index? Link to comment
Moderators IIYAMA Posted October 30, 2014 Moderators Share Posted October 30, 2014 Take a look at this, an example structure: local timers = { [userdata] = { -- [userdata] ["playerData1"]={ -- ["playerData1"] data1,-- [1] getTickCount() -- [2] }, ["playerData2"]={ data1, getTickCount() }, ["playerData3"]={ data1, getTickCount() } }, } --[[ timers [userdata] ["playerData1"] [1] timers[userdata]["playerData1"][1] = data1 timers[userdata]["playerData1"][2] = getTickCount() ]] and how does this work? local bufferedTable = timers[player] -- we index ones if not bufferedTable then -- we check if there is a player at this index, if not we set it. timers[player]={} -- < this table will be in the variable bufferedTable at the line below bufferedTable = timers[player] -- we index ones again and making sure the player has it's table anyway. end So what do we have now? The table structure is now: local timers = { [userdata] = {} -- = bufferedTable } These are some basic stuff, the base of our data storage. Now lets add our archive. bufferedTable["playerData1"] = {data1,getTickCount()} So what do we have now? The table structure is now: local timers = { [userdata] = { -- = bufferedTable ["playerData1"] = { data1,getTickCount() } } } Lets do it again: bufferedTable["playerData2"] = {data1,getTickCount()} The table structure is now: local timers = { [userdata] = { -- = bufferedTable ["playerData1"] = { data1,getTickCount() }, ["playerData2"] = { data1,getTickCount() } } } You have to see it as layers. Or like the folder structure at your windows pc: Like: D:\Program Files (x86)\MTA San Andreas 1.4 Lets make a table of it: --D:\Program Files (x86)\MTA San Andreas 1.4 myMTALocation = { ["D:"]={--D: ["Program Files (x86)"] = {--\Program Files (x86) ["MTA San Andreas 1.4"] = {--\MTA San Andreas 1.4 ["mods"]={},--\mods ["MTA"]={},--\MTA ["screenshots"]={},--\screenshots ["server"]={},--\server ["skins"]={},--\skins "Multi Theft Auto.exe",--\Multi Theft Auto.exe "Uninstall.exe"--\Uninstall.exe } } } } Link to comment
Drakath Posted October 30, 2014 Author Share Posted October 30, 2014 Thanks, the tables work fine now. There is one more problem, when my table starts from an index higher than 1, it stops working. I use JSON. For example, when myTable is [ { "2": 175614 } ], JmyTable = fromJSON(myTable) JmyTable[2] is nil. Link to comment
Moderators IIYAMA Posted October 30, 2014 Moderators Share Posted October 30, 2014 https://wiki.multitheftauto.com/wiki/FromJSON Note: Indices of a JSON object such as "1": "cat" are being returned as string, not as integer. [ { "2": 175614 } ] JmyTable[2] local index = 2 JmyTable[tostring(index)] JmyTable["2"] Link to comment
Woovie Posted October 31, 2014 Share Posted October 31, 2014 https://wiki.multitheftauto.com/wiki/FromJSONNote: Indices of a JSON object such as "1": "cat" are being returned as string, not as integer. [ { "2": 175614 } ] JmyTable[2] local index = 2 JmyTable[tostring(index)] JmyTable["2"] If the table was just [[some_number, 175614]] then you could use JmyTable[2] to pull the number. Link to comment
Feche1320 Posted November 1, 2014 Share Posted November 1, 2014 How can I trigger a server event via client side when a player leaves? addEventHandler("onClientResourceStop", getResourceRootElement(), function() triggerServerEvent("savePlayerData", localPlayer) end ) That should do it. Link to comment
Anubhav Posted November 1, 2014 Share Posted November 1, 2014 It won't work. Server sided script will be un-loaded until the time! Link to comment
Feche1320 Posted November 1, 2014 Share Posted November 1, 2014 It won't work. Server sided script will be un-loaded until the time! He didn't say when the server shutdowns. He just wants to save the data when a player leaves from the server, onClientResourceStop gets called when player disconnects/quits from MTA. Link to comment
Anubhav Posted November 1, 2014 Share Posted November 1, 2014 It won't work. Server sided script will be un-loaded until the time! He didn't say when the server shutdowns. He just wants to save the data when a player leaves from the server, onClientResourceStop gets called when player disconnects/quits from MTA. You misunderstood me, I meant that server sided script will be un-loaded before client. So it will give out a error! Link to comment
Feche1320 Posted November 1, 2014 Share Posted November 1, 2014 It won't work. Server sided script will be un-loaded until the time! He didn't say when the server shutdowns. He just wants to save the data when a player leaves from the server, onClientResourceStop gets called when player disconnects/quits from MTA. You misunderstood me, I meant that server sided script will be un-loaded before client. So it will give out a error! The script that I wrote goes client-side. 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