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

I've made a leaderboard save script, how do I make a load script?

I've made a leaderboard saving script, but I want to make a load script aswell. How would I do it?

(Here is the saving script)



local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")
local ds2 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local gems = Instance.new("IntValue", folder)
 gems.Name = "Gems"
 local cash = Instance.new("IntValue", folder)
 cash.Name = "Cash"
 
 gems.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, gems.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)
 
 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)
 
 cash.Changed:connect(function()
  ds2:SetAsync(plr.UserId, cash.Value)
 end)
end)

1 answer

✓ Accepted answer

With datastores you can use a function called GetAsync() it requires only one argument, which would be in your case plr.UserId,

This might work, I may be wrong.

ds1:GetAsync(plr.UserId)
ds2:GetAsync(plr.UserId)

Also I wouldn't recommend saving the value to the data store every time it's changed as it can cause queues to be built up, I would recommenced having data save when the player leaves, or save every interval (5 minutes -- 300 seconds)

2 comments

0

Where do I put it? Doesn't seem to work..

0

where you put it is up to you, but you should put it after they join or such, you can read more here: https://developer.roblox.com/api-reference/function/GlobalDataStore/GetAsync

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