How do I add more variables to my datastore without reseting them

So I tried some times to add a kills variable to my datastore script which already saves and loads one variable called strength, so that when a player kills an npc or a player the kills amount increases by 1 and then saves and loads without changing the value of other variables or resetting all of them to 0.

The problem is that when I tried adding one more variable, the game either reset my strength and kills to 0 or either doesn’t showcase both values, and I really want to add this fearture to my game, becuase it can engage more and so on.
Here is the script which I mentioned:

-- IMPORTANT --

-- For this to save you must go to Game Settings, Security, and enable Acess to API Services --

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")

game.Players.PlayerAdded:Connect(function(Player)
    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
    Leaderstats.Parent = Player
    local strength = Instance.new("IntValue")
    strength.Name = "strength"
    strength.Value = 0
	strength.Parent = Leaderstats

    
    local Data = DataStore:GetAsync(Player.UserId)
    if Data then
        strength.Value = Data
    end
    
	
    end)
game.Players.PlayerRemoving:Connect(function(Player)
    DataStore:SetAsync(Player.UserId, Player.leaderstats.strength.Value)
    end)

And here is its location in the game:
Captura de pantalla 2024-12-07 152111

  1. Well, for implementing that, I tried a bunch of things: whatching many tutorials, looking at simmilar forums for a solution, trying some scripts on the toolbox, and even recoding the script above myself (I know some stuff), but nothing worked. So if someone can help me then I’ll thank.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

For starters, probably use tables instead of saving a single value, and use UpdateAsync.

What does this mean? When saving data you can save just about anything, that includes tables, of which can have multiple values, ex:

Local DefaultData = {
   strength = 0,
   kills = 0,
   IsCool = false,
   PlrColor = color3.new(255,255,255),
   Pets = {
      Frog = {
         Level = 500
      }
   }
}

When saving a table/dictionary, this allows us to again, save multiple values.
How do we save a table? Its the same as saving any other value really.

local PlayerData = DefaultData -- You would replace this with the players data.

Datastore:SetAsync(UserId,PlayerData)

We can also use something called UpdateAsync to save data, instead of just overriding the data, which works, it instead updates it, which is like better somehow, I dont remember gonna be real.

DataStore:UpdateAsync(UserId, function(pastData)
    return PlayerData -- Saves PlayerData
end)

If you want more info on UpdateAsync I recommend this tutorial:

Along with that, if you want data to be less tedious to work with, there are a lot of developer made modules that allow you to handle data, one of which is:

of which comes with lots of built in functions to help you reduce data loss and stuff like that.
If you dont want to use the module, I would still recommend looking at the code so you can maybe understand how they go about using it, so you can learn a thing or two!

1 Like