Jump to content

Using tables to save data.


zixxonx

Recommended Posts

Hello, i'm really struggling with saving players data in tables. Let's for example, i have that table:

data = {} 

When player joins i want to set some things for him. I'm doing it by:

data[source] = {deaths = 0, kills=0} 

Now, when player kills somebody i want to increase his kills.

kills = data[source].kills + 100 
data[source] = {kills= kills} 

But this is deleting the DEATHS value from table. How can i achieve it in other way? Thanks.

Link to comment
  
data[source].kills = kills -- You're only changing the value of the key "kills". 
data[source] = {kills= kills} -- You're actually replacing the entire table here. 
  

  
-- An example 
local table = {["value1"] = 0, value2 = 0} 
table.value1 = 0 
table["value2"] = 0 
  

Link to comment

Oh well, thanks. So when i create the table for player

data[source] = {things here} 

I need to put here all the variables i will be using in future for this table? I mean if i do it like this:

    data[source] = {deaths = 0, kills=0} 

And later on i wanna also add CASH here, it's possible? Or maybe even like in PAWN i could do like this:

enum UserData

{

ID,

Admin,

Gender,

SkinID,

MaskID

}

And later then

UserData[playerid][skinID] = 666

Link to comment
  • MTA Team
  
gUserData = { } 
  
function resetPlayer(player) 
    gUserData[player] = { 
        id = false, 
        admin = false, 
        gender = 'm', 
        skin = 0, 
        mask = 0, 
        money = 0, 
        kills = 0, 
        deaths = 0, 
    } 
end 
  
addEventHandler("onPlayerJoin", root, 
    function () 
        resetPlayer(source) 
    end 
) 
  
addEventHandler("onPlayerQuit", root, 
    function () 
        -- Release the memory 
        gUserData[source] = nil 
    end 
) 
  
addEventHandler("onResourceStart", resourceRoot, 
    function () 
        local playerlist = getElementsByType("player") 
        for i = 1, #playerlist do 
            resetPlayer(playerlist[i]) 
        end 
    end 
) 
  
addEventHandler("onPlayerWasted", root, 
    function (totalAmmo, killer) 
        if isElement(killer) then 
            gUserData[killer].kills = gUserData[killer].kills + 1 
        end 
        gUserData[source].deaths = gUserData[source].deaths + 1 
    end 
) 
  

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