Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local datastoreservice = game:GetService("DataStoreService")
- local Mydatastore = datastoreservice:GetDataStore("Session1")
- local PlayerData = game:GetService("ServerStorage").PlayerData:Clone()
- local loaded = {}
- game.Players.PlayerAdded:Connect(function(plr)
- local success, value = pcall(Mydatastore.GetAsync, Mydatastore, plr.UserId)
- if success == false then warn(value) return end
- local data = value or {} -- if data is data is equal to something, or the player has some data then it'll get it. If not then it'll make a empty table.
- print("Loaded:", value)
- local Data1 = Instance.new("Folder")
- Data1.Name = "Data1"
- Data1.Parent = PlayerData
- local Data2 = Instance.new("Folder")
- Data2.Name = "Data2"
- Data2.Parent = PlayerData
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = PlayerData
- local ShirtSelection = Instance.new("IntValue")
- ShirtSelection.Name = "Shirt"
- ShirtSelection.Value = 0
- ShirtSelection.Parent = Data2
- local PantsSelection = Instance.new("IntValue")
- PantsSelection.Name = "Pants"
- PantsSelection.Value = 0
- PantsSelection.Parent = Data2
- for i, folder in PlayerData:GetChildren() do
- local subdata = data[folder.Name] or {} -- the subdata will try to equal to the data's name, but if it is nil then it'll make a empty table
- local clone = folder:Clone()
- for i, child in clone:GetChildren() do
- child.Value = subdata[child.Name] or child.Value-- getting whatever int or bool value inside the playerdata folders, and equalling it to the current value
- end
- clone.Parent = plr
- end
- loaded[plr] = true
- end)
- game.Players.PlayerRemoving:Connect(function(Player)
- if loaded[Player] == nil then return end
- local data ={}
- for i, folder in PlayerData:GetChildren() do
- local subData = {}
- for i, child in Player[folder.Name]:GetChildren() do
- subData[child.Name] = child.Value
- end
- data[folder.Name] = subData
- Player[folder.Name]:Destroy()
- end
- local success, value = pcall(Mydatastore.SetAsync, Mydatastore, Player.UserId, data)
- print("Saved:", data)
- loaded[Player] = nil
- end)
- game:BindToClose(function()
- while next(loaded) ~= nil do
- task.wait()
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement