Jump to content

Saving client data


Drakath

Recommended Posts

  • Moderators

@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

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

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

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

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

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

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