How to add more leaderstats to datastore?

Hi,

I have a datastore, but it only saves my money and not stamina, hunger and wood.

How do I add other stats to it?

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local plrkey = "id_"..plr.UserId
    local saves = plr.leaderstats.Money
    local Save = ds:GetAsync(plrkey)

    if Save then
        saves.Value = Save[1]
    else
        local numbersave = {saves.Value}
        ds:SetAsync(plrkey, numbersave)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Money.Value})
end)

game:BindToClose(function() for _, plr in pairs(game.Players:GetPlayers()) do ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Money.Value}) end end)

Hi!

I fixed it

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()

	local key = "user_"..plr.UserId

	local saveValue1 = plr.leaderstats.Money
	local saveValue2 = plr.leaderstats.Wood

	local getSaved = ds:GetAsync(key)

	if getSaved then
		saveValue1.Value = getSaved[1]
		saveValue2.Value = getSaved[2]
	else
		local NumbersForSaving = {saveValue1.Value, saveValue2.Value}

		ds:GetAsync(key, NumbersForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local saveValue1 = player.leaderstats.Money
	local saveValue2 = player.leaderstats.Wood

	ds:SetAsync("user_"..player.UserId, {saveValue1.Value, saveValue2.Value})
end)

game:BindToClose(function() for _, plr in pairs(game.Players:GetPlayers()) do ds:SetAsync("id_"..plr.UserId, {plr.leaderstats.Money.Value}) end end)
1 Like