Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local data = game:GetService("DataStoreService"):GetOrderedDataStore("Candy")
- local function SaveData(plr,t)
- local tries = 0
- local max = 5
- local success
- while not success and tries < max do
- success = pcall(function()
- if plr and t then
- data:SetAsync(plr.UserId,t.Value)
- end
- end)
- tries = tries + 1
- wait(tries)
- end
- end
- game.Players.PlayerAdded:Connect(function(plr)
- local stat
- local function GetStats()
- wait(0.1)
- if not plr:FindFirstChild("Stats") then
- wait(1.5)
- GetStats()
- else
- stat = plr:WaitForChild("Stats")
- return true;
- end
- end
- GetStats()
- local function CreateStat(class,name,parent)
- local i = Instance.new(class)
- i.Name = tostring(name)
- i.Parent = parent
- return i
- end
- local money = CreateStat("IntValue","🍬Candys",stat)
- wait(0.03)
- local tries = 0
- local max = 5
- local success
- local SavedLevel
- while not success and tries < max do
- success = pcall(function()
- SavedLevel = data:GetAsync(plr.UserId)
- end)
- wait(tries)
- tries = tries + 1
- end
- if SavedLevel then
- money.Value = SavedLevel
- else
- SaveData(plr,money)
- end
- local autoSaveInterval = 180
- coroutine.resume(coroutine.create(function()
- while wait(autoSaveInterval) do
- SaveData(plr,money)
- end
- end))
- end)
- game.Players.PlayerRemoving:Connect(function(plr)
- if plr:FindFirstChild("Stats") and plr.Stats:FindFirstChild("🍬Candys") then
- SaveData(plr,plr.Stats["🍬Candys"])
- end
- end)
- if not game:GetService("RunService"):IsStudio() then
- game:BindToClose(function()
- for _,plr in pairs(game:GetService("Players")) do
- if plr:FindFirstChild("Stats") and plr.Stats:FindFirstChild("🍬Candys") then
- SaveData(plr,plr.Stats["🍬Candys"])
- end
- end
- wait(10)
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement