Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ds = game:GetService'DataStoreService':GetDataStore('Data-1')
- folder = game.ReplicatedStorage:FindFirstChild('PlayerData'):FindFirstChild('DataTemplate')
- local loadedOk = setmetatable({}, {__mode = 'k'});
- function save(plr, Type)
- local key = tostring(plr.userId)
- local data = {}
- for iii,v in pairs(game.ReplicatedStorage:FindFirstChild('PlayerData'):FindFirstChild(plr.Name):GetChildren())do
- data[tostring(v)] = v.Value
- end
- local success, message = pcall(function()
- ds:UpdateAsync(key, function()
- return data
- end)
- end)
- if not success then
- print(plr.Name.." An error occurred: " .. message)
- return false
- else
- if Type == "Leaving" then
- game.ReplicatedStorage:FindFirstChild('PlayerData'):FindFirstChild(plr.Name):Destroy()
- return true
- else
- return true
- end
- end
- end
- game.Players.PlayerAdded:Connect(function(plr)
- wait(0.1)
- local data
- local key = tostring(plr.userId)
- --
- local f = folder:clone()
- f.Name = tostring(plr)
- f.Parent = game.ReplicatedStorage:FindFirstChild('PlayerData')
- data = ds:GetAsync(key) or {}
- for i,v in pairs(data)do
- if f:FindFirstChild(i) then
- f:FindFirstChild(i).Value = v
- end
- end
- loadedOk[plr] = true;
- end)
- game:GetService'Players'.PlayerRemoving:Connect(function(plr)
- if (loadedOk[plr]) then
- if save(plr, "Leaving") == false then
- print("Failed to save "..plr.Name.."s data")
- end
- end
- end)
- game:BindToClose(function()
- wait(10)
- end)
- while wait(120) do
- for _,plr in pairs(game.Players:GetPlayers()) do
- if (loadedOk[plr]) then
- if save(plr, "AutoSave") == false then
- print("Failed to save "..plr.Name.."s data")
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement