Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ds = game:GetService'DataStoreService':GetDataStore('Data-1')
- folder = game:GetService("ServerStorage"):FindFirstChild("PlayerData"):FindFirstChild("DataTemplate")
- local loadedOk = setmetatable({}, {__mode = 'k'});
- function save(plr, saveType)
- local key = tostring(plr.userId)
- local data = {}
- for iii,v in pairs(game:GetService("ServerStorage"):FindFirstChild("PlayerData"):FindFirstChild(plr.Name):GetChildren())do
- if v:FindFirstChild("Equipped") then
- for ii,vv in pairs(v:GetChildren())do
- data[tostring(v)] = {
- v.Value,
- v.Equipped.Value
- }
- end
- else
- data[tostring(v)] = {
- v.Value
- }
- end
- 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 saveType == "Leaving" then
- game:GetService("ServerStorage"):FindFirstChild("PlayerData"):FindFirstChild(plr.Name):Destroy()
- end
- return true
- 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:GetService("ServerStorage"):FindFirstChild("PlayerData")
- data = ds:GetAsync(key) or {}
- for i,v in pairs(data)do
- if f:FindFirstChild(i) ~= nil then
- if f:FindFirstChild(i):FindFirstChild("Equipped") then
- print(v[1])
- print(v[2])
- f:FindFirstChild(i).Value = v[1]
- f:FindFirstChild(i):FindFirstChild("Equipped").Value = v[2]
- else
- print(v[1])
- f:FindFirstChild(i).Value = v[1]
- end
- end
- end
- loadedOk[plr] = true;
- for i,v in pairs(game:GetService("ServerStorage"):FindFirstChild("PlayerData"):FindFirstChild(plr.Name):GetChildren()) do
- v:Clone().Parent = plr
- end
- for _,v in pairs(game:GetService("ServerStorage"):FindFirstChild("PlayerData"):FindFirstChild(plr.Name):GetChildren()) do
- v.Changed:connect(function()
- plr:FindFirstChild(v.Name).Value = game:GetService("ServerStorage"):FindFirstChild("PlayerData"):FindFirstChild(plr.Name):FindFirstChild(v.Name).Value
- end)
- end
- 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(5)
- 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