Setting saved DataStore value

I was wondering how exactly to assign my leaderstats value that I have placed that works into this DataStore script so that it saves. The script already prints well and works, but there’s no value attached yet. Here’s a file to the game, so you can see where and what the leaderstats value are:
updated.rbxl (225.4 KB)
And here’s the script:

local DS = game:GetService("DataStoreService")
local Cash = DS:GetDataStore("FirstStore")
local FirstKey = Cash:GetAsync("FirstKey")

if FirstKey then
	print("Found: " .. FirstKey)
	
else
	print("No key found. Making first key.")
	Cash:SetAsync("FirstKey", "Here's the data")
end
2 Likes

Maybe try using a table or have instances where you can directly index them. Also try to avoid using multiple GetDataStore’s, try to use just 1. From the looks, it seems like for every data you add, you will be saving by doing DS:GetDataStore(‘…’), avoid that.

2 Likes

Here’s an example of a DataStore. You wouldn’t create a new DataStore for every value because you can store a table of values in a DataStore, not just 1 value. Also, I added another value to it, “Coins” to show how you would do multiple.

DataStore:

local DS = game:GetService("DataStoreService"):GetDataStore("Economy")

game.Players.PlayerAdded:Connect(function(plr) -- When a player joins the server
	wait(1) -- To let leaderboard load
	local plrkey = plr.userId
	local stat1 = plr.leaderstats.Cash
    local stat2 = plr.leaderstats.Coins
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then -- if player has already played the game
		stat1.Value = GetSaved[1]
		stat2.Value = GetSaved[2]
	else -- if they're a new player
		local NumbersForSaving = {stat1.Value, stat2.Value}
		DS:SetAsync(plrkey, NumbersForSaving)
	end
end)

function Update(plr) -- Update stats
	DS:SetAsync(plr.UserId, {plr.leaderstats.Cash.Value, plr.leaderstats.Coins.Value})
	end)
end

while wait(30) do
	for _, plr in ipairs(game:GetService("Players"):GetChildren()) do -- for every player
		Update(plr) -- Add plr paramater so func knows which player
	end
end

game.Players.PlayerRemoving:Connect(function(plr) -- for player that's leaving game
	Update(plr)
end)

Also, you can only have 1 leaderboard which you can store multiple values in as well. You put money.Value = 0 in a script, so just leave that line out. Roblox already gives it a default value of 0, so there is no need to do this, and it can mess up the datastore sometimes. cash.Parent = leaderstats This line is also not needed, as you can set it’s parent in the instance function: local cash = Instance.new("IntValue", leaderstats)

Leaderboard:

game.Players.PlayerAdded:Connect(function(player) -- When they join the server
    local leaderstats = Instance.new("Folder", player) -- Create folder for leaderboard
    leaderstats.Name = "leaderstats"

    local cash = Instance.new("IntValue", leaderstats) -- Cash stat
    cash.Name = "Cash"

    local coins = Instance.new("IntValue", leaderstats) -- Coins stat
    coins.Name = "Coins"
end)

@Urxpaly45 It seems that the leaderboards have shown up, but it is still not saving.

1 Like

I would recommend wrapping the datastore calls in pcalls in the event of an error. Here is a guide if you need one.

1 Like

I dont understand why you’re using SetAsync if they don’t have any data.
You can just save it on a interval, BindToClose and when a player leaves.

There’s no need to set empty data if they don’t have any currently.
And, wrap your calls in a pcall().

Do you have the game published with API access?

1 Like