Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function onPlayerEntered(newPlayer)
- local datafolder = Instance.new("Folder")
- datafolder.Parent = game.ReplicatedStorage.DataHolder
- datafolder.Name = newPlayer.UserId
- local vs = game.ReplicatedStorage.DataHolder.Data:clone()
- vs.Parent = game.ReplicatedStorage.DataHolder[newPlayer.UserId]
- local Data = vs:GetChildren()
- local S1 = {}
- for i=1,#Data do
- local n = Data[i].Name
- S1[n] = Data[i].Value
- end
- --Get Data--
- if newPlayer.Name ~= "Player" then
- local DataStore = game:GetService("DataStoreService"):GetDataStore(newPlayer.UserId)
- if DataStore:GetAsync("Data") == nil then
- DataStore:SetAsync("Data", S1)
- end
- S1 = DataStore:GetAsync("Data")
- end
- --Safely Load Data--
- local vv = vs:GetChildren()
- for i=1,#vv do
- local n = vv[i].Name
- --print(S1[n])
- if S1[n] ~= nil then
- vv[i].Value = S1[n]
- end
- end
- end
- game.Players.PlayerAdded:Connect(onPlayerEntered)
- for _,player in pairs(game.Players:GetChildren()) do
- onPlayerEntered(player)
- end
- game.Players.PlayerRemoving:Connect(function(p)
- --Save Data--
- local DataStore = game:GetService("DataStoreService"):GetDataStore(p.userId)
- local Data = game.ReplicatedStorage.DataHolder[p.UserId].Data:GetChildren()
- local s1 = {}
- for i=1,#Data do
- local n = Data[i].Name
- s1[n] = Data[i].Value
- end
- local success, err = pcall(function()
- DataStore:SetAsync("Data", s1)
- end)
- if success then
- print("Success!")
- end
- -- DataStore:SetAsync("Data",s1)
- game.ReplicatedStorage.DataHolder[p.UserId]:Destroy()
- print("Saved "..p.Name)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement