Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Cash leaderboard with data store not work?

Asked by 5 years ago
Edited 5 years ago

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.

0
Have you tried print debugging it yet? It may be tedious, but works almost every time for me. Crazycat4360 115 — 5y
0
How to do that? XxxLloyd061302xxX 5 — 5y
0
userId is deprecated mate, also are you going to make another tutorial where you use a Script instead of a LocalScript User#19524 175 — 5y

1 answer

Log in to vote
0
Answered by
ABK2017 406 Moderation Voter
5 years ago

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)
0
I think data ready is deprecated. XxxLloyd061302xxX 5 — 5y
0
It is indeed. Crazycat4360 115 — 5y
0
So is LoadNumber, and findFirstChild. User#19524 175 — 5y
Ad

Answer this question