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

How to get this savable leaderboard working? [closed]

Asked by 6 years ago

I am creating a game and i really needed a a savable leaderboard i finally found one but since i started using it this error message came up saying i have to break or kill the script and its crashing roblox studio.

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)

0
free model eh User#19524 175 — 6y

Closed as Not Constructive by User#19524

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 6 years ago

The reason your script is crashing is because without a wait() it lets the script run a lot of times very quickly which will cause studio to crash so you will need to add a wait into it somewhere like this:

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)
wait()
 end)
end)

If this doesn't work, I didn't try it so.

Ad