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)
COMMUNITY QUESTION
This leaderboard saves data when I am testing it in studio but not when I play it on roblox?
2 answers
Try putting wait() on top of the script, that should fix it.
Try using pcall. Also please do not use Instance.new with parent parameter, it's deprecated. Same with connect, use Connect instead.
1 comment
0
deprecated for connect and the parent paramerter mean nothing. These will probley never being going away as so meny users use those functions.
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Good habit to put datastore in modulescripts, just saying.
Try putting the :GetAsync() and :SetAsync() methods in a pcall
how to I put them in a pcall