Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Cash leaderboard with data store not work?

So I am making a cash leaderboard with a Data store. Now when I test it, it doesn't work. It works before but now it doesn't. Here is the script:

-- Savable leaderboard?
local ds = game:GetService("DataStoreService"):GetDataStore("Cash1112")

game.Players.PlayerAdded:Connect(function(player)
	local key = "cash-"..player.userId
	local folder = Instance.new("Folder",player)
	folder.Name = "leaderstats" -- Must be lowercase
	local currency = Instance.new("IntValue",folder)
	currency.Name = "Cash" -- Name of your currency
	currency.Value = 0 -- Starting Cash
	
	local save = ds:GetAsync(key)
	if save then
		currency.Value = save[1]
	else
		local tab = {currency.Value}
		ds:SetAsync(key,tab)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = "cash-"..player.userId
	local currency = player.leaderstats:FindFirstChild("Cash") -- Currency name. Must match with the top
	
	local tab = {currency.Value}
	ds:SetAsync(key,tab)
	
end)

Thanks.

3 comments

0

Have you tried print debugging it yet? It may be tedious, but works almost every time for me.

0

How to do that?

0

userId is deprecated mate, also are you going to make another tutorial where you use a Script instead of a LocalScript

1 answer

I have a separate script for the actual leaderboard but I use this script in ServerScriptStorage and it's been working if this helps at all...

function onPlayerEntered(player)
wait()-- Change to wait for player longer.
player:WaitForDataReady()
repeat wait() until player:FindFirstChild("leaderstats")
if player.DataReady then
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
local ScoreLoaded = player:LoadNumber(score[i].Name)
wait()
if ScoreLoaded ~= 0 then
score[i].Value = ScoreLoaded
end
end
end
end
end

function onPlayerLeaving(player)
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
player:SaveNumber(score[i].Name,score[i].Value)
end
end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerLeaving)

3 comments

0

I think data ready is deprecated.

0

It is indeed.

0

So is LoadNumber, and findFirstChild.

This discussion is preserved as it was. New answers, comments, and votes are closed.