Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if game:GetService("RunService"):IsStudio() then
- Data = game:GetService("DataStoreService"):GetDataStore("PeaaaaaaaaaaaaaaaaaaaaaaasStudio")
- print("Currently in studio")
- else
- Data = game:GetService("DataStoreService"):GetDataStore("Peaaaaaaaaaaaaaaaaaaaaaaas")
- print("not in studio")
- end
- counter = 0
- remote = game:GetService("ReplicatedStorage").error
- dataErrored = false
- function getData(plr)
- local success, errormsg = pcall(function()
- data = Data:GetAsync(plr.UserId)
- end)
- if success then
- counter = 0
- return data
- else
- if counter >= 4 then
- warn("Couldnt get user data")
- return "dataError"
- end
- wait(.1)
- counter = counter + 1
- getData(plr)
- end
- end
- function saveData(plr, save)
- local success, errormsg = pcall(function()
- data = Data:SetAsync(plr.UserId, save)
- end)
- if success then
- counter = 0
- print("saved data")
- return data
- else
- wait(.1)
- saveData(plr)
- end
- end
- game.Players.PlayerAdded:Connect(function(plr)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = 'leaderstats'
- leaderstats.Parent = plr
- local money = Instance.new("NumberValue")
- money.Name = "Cash"
- money.Parent = leaderstats
- local stat = Instance.new("StringValue")
- stat.Name = 'Status'
- stat.Parent = leaderstats
- stat.Value = "None"
- dataSave = {money.Value, stat.Value}
- local data = getData(plr)
- if data == "dataError" then
- remote:FireClient(plr)
- print("Error")
- dataErrored = true
- elseif data then
- money.Value = data[1]
- stat.Value = data[2]
- print("Data loaded")
- else
- warn("New user.")
- money.Value = 0
- end
- end)
- game.Players.PlayerRemoving:Connect(function(plr)
- if not dataErrored then
- print("Saving")
- saveData(plr, dataSave)
- else
- print("Data has errored")
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement