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

How do I make this leaderstats script save to the Datastore?

Asked by 4 years ago

I have a script in one of my games that handles money being given out every 5 minutes, and I need it to save to the datastore so when the player rejoins the game after buying/earning the money they keep it, instead of their money counter resetting.

The code is here to help you understand what I am talking about:

01game.Players.PlayerAdded:Connect(function(Plr)
02    local Stats = Instance.new("Folder", Plr)
03    Stats.Name = "leaderstats"
04 
05    local Cash = Instance.new("IntValue", Stats)
06    Cash.Name = "Cash"
07    Cash.Value = 0
08 
09    local DS = game:GetService("DataStoreService"
10 
11    game.Players.PlayerRemoving:Connect(function()
12        DS:SetAsync(Plr.userId.."_DS", Cash.Value)
13    end)
14 
15    while true do
View all 21 lines...

As you can see in the script above I tried getting an understanding of the Datastore service

1local DS = game:GetService("DataStoreService"
2 
3game.Players.PlayerRemoving:Connect(function()
4    DS:SetAsync(Plr.userId.."_DS", Cash.Value)
5end)

But, that did not work. Could I get some help on this?

0
You never actually retrieved and set their data. Read the GlobalDataStore documentation or watch a tutorial, preferably by TheDevKing. Ziffixture 6913 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

YOU NEED TO COPY THIS ALL DOWN. SOME OF IT WONT COME UP AS A SCRIPT BUT COPY AND PASTE THIS ALL INTO A NEW SCRIPT IN SERVER SCRIPT SERVICE

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData") game.Players.PlayerAdded:Connect(function(plr) wait() local plrkey ="id_"..plr.userId local save1 = plr.leaderstats.Kills

1local GetSaved = DS:GetAsync(plrkey)
2if GetSaved then
3    save1.Value = GetSaved[1]
4else
5    local NumberForSaving = {save1.Value}
6    DS:GetAsync(plrkey, NumberForSaving)
7end

end)

game.Players.PlayerRemoving:Connect(function(plr) DS:SetAsync("id_"..plr.userId, {plr.leaderstats.Cash.Value}) end)

0
Amazing! Thanks for the code, Datastore on roblox has always stumped me. Excellent work! ParSoft 2 — 4y
Ad

Answer this question