Advertisement
Guest User

DataStore Scr

a guest
Apr 10th, 2023
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. local datastoreservice = game:GetService("DataStoreService")
  2. local Mydatastore = datastoreservice:GetDataStore("Session1")
  3. local PlayerData = game:GetService("ServerStorage").PlayerData:Clone()
  4.  
  5. local loaded = {}
  6.  
  7. game.Players.PlayerAdded:Connect(function(plr)
  8.    
  9.  
  10.     local success, value = pcall(Mydatastore.GetAsync, Mydatastore, plr.UserId)
  11.     if success == false then warn(value) return end
  12.    
  13.     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.
  14.     print("Loaded:", value)
  15.    
  16.     local Data1 = Instance.new("Folder")
  17.     Data1.Name = "Data1"
  18.     Data1.Parent = PlayerData
  19.    
  20.     local Data2 = Instance.new("Folder")
  21.     Data2.Name = "Data2"
  22.     Data2.Parent = PlayerData
  23.    
  24.     local leaderstats = Instance.new("Folder")
  25.     leaderstats.Name = "leaderstats"
  26.     leaderstats.Parent = PlayerData
  27.    
  28.    
  29.     local ShirtSelection = Instance.new("IntValue")
  30.     ShirtSelection.Name = "Shirt"
  31.     ShirtSelection.Value = 0
  32.     ShirtSelection.Parent = Data2
  33.    
  34.     local PantsSelection = Instance.new("IntValue")
  35.     PantsSelection.Name = "Pants"
  36.     PantsSelection.Value = 0
  37.     PantsSelection.Parent = Data2
  38.    
  39.     for i, folder in PlayerData:GetChildren() do
  40.         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
  41.         local clone = folder:Clone()
  42.        
  43.         for i, child in clone:GetChildren() do
  44.             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
  45.         end
  46.         clone.Parent = plr
  47.     end
  48.     loaded[plr] = true
  49. end)
  50.  
  51.  
  52.  
  53.  
  54.  
  55. game.Players.PlayerRemoving:Connect(function(Player)
  56.    
  57.     if loaded[Player] == nil then return end
  58.     local data ={}
  59.    
  60.     for i, folder in PlayerData:GetChildren() do
  61.         local subData = {}
  62.         for i, child in Player[folder.Name]:GetChildren() do
  63.             subData[child.Name] = child.Value
  64.         end
  65.         data[folder.Name] = subData
  66.         Player[folder.Name]:Destroy()
  67.     end
  68.     local success, value = pcall(Mydatastore.SetAsync, Mydatastore, Player.UserId, data)
  69.     print("Saved:", data)
  70.     loaded[Player] = nil
  71. end)
  72.  
  73.  
  74. game:BindToClose(function()
  75.     while next(loaded) ~= nil do
  76.         task.wait()
  77.     end
  78. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement