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

This leaderboard saves data when I am testing it in studio but not when I play it on roblox? [closed]

Asked by 5 years ago

local DSService = game:GetService("DataStoreService"):GetDataStore("Currency")
local Saving = 0

game:BindToClose(function()
    repeat wait() until Saving <= 0
end)

game:GetService("Players").PlayerAdded:connect(function(plr)
    local leader = Instance.new("Folder",plr)
    leader.Name = "leaderstats"

        local Credits = Instance.new("IntValue",leader)
        Credits.Name = "Credits"

        local Diamonds = Instance.new("IntValue",leader)
        Diamonds.Name = "Diamonds"

    local savedData = DSService:GetAsync(plr.UserId.."-Currency")

    if savedData then
        Credits.Value = savedData[1]
        Diamonds.Value = savedData[2]
    else
        local values = {Credits.Value, Diamonds.Value}
        DSService:SetAsync(plr.UserId.."-Currency",values)
    end
end)

game:GetService("Players").PlayerRemoving:connect(function(plr)
    Saving = Saving + 1
    local ValuesToSave = {plr.leaderstats.Credits.Value, plr.leaderstats.Diamonds.Value}
    DSService:SetAsync(plr.UserId.."-Currency", ValuesToSave)
    Saving = Saving - 1
end)


0
Good habit to put datastore in modulescripts, just saying. TiredMelon 405 — 5y
0
Try putting the :GetAsync() and :SetAsync() methods in a pcall Trainsparency 39 — 5y
0
how to I put them in a pcall jakebball2014 84 — 5y

Marked as Duplicate by arshad145, User#21908, and User#19524

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

2 answers

Log in to vote
0
Answered by
Echtic 128
5 years ago

Try putting wait() on top of the script, that should fix it.

Ad
Log in to vote
0
Answered by 5 years ago

Try using pcall. Also please do not use Instance.new with parent parameter, it's deprecated. Same with connect, use Connectinstead.

0
deprecated for connect and the parent paramerter mean nothing. These will probley never being going away as so meny users use those functions. outlook1234567890 115 — 5y