Advertisement
3DCreator

DataHandler

Apr 6th, 2022
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local DataStoreService = game:GetService("DataStoreService")
  2. local Players = game:GetService("Players")
  3. local StatsStore = DataStoreService:GetDataStore("StatsStore")
  4.  
  5. Players.PlayerAdded:Connect(function(Player)
  6.     local Leaderstats = Instance.new("Folder", Player)
  7.     Leaderstats.Name = "leaderstats"
  8.     local Points = Instance.new("NumberValue", Leaderstats)
  9.     Points.Name = "Points"
  10.     local Wins = Instance.new("IntValue", Leaderstats)
  11.     Wins.Name = "Wins"
  12.     local Rank = Instance.new("StringValue", Leaderstats)
  13.     Rank.Name = "Rank"
  14.     local Data = StatsStore:GetAsync(Player.UserId)
  15.    
  16.     if Data then
  17.         Points.Value = Data.Points or Data[1]
  18.         Wins.Value = Data.Wins or Data[2]
  19.         Rank.Value = Data.Rank or Data[3]
  20.     elseif not Data then
  21.         Points.Value = 0
  22.         Wins.Value = 0
  23.         Rank.Value = "Unranked"
  24.     end
  25. end)
  26.  
  27. Players.PlayerRemoving:Connect(function(Player)
  28.     local Leaderstats = Player:FindFirstChild("leaderstats")
  29.     if Leaderstats then
  30.         local Points = Leaderstats:WaitForChild("Points")
  31.         local Wins = Leaderstats:WaitForChild("Wins")
  32.         local Rank = Leaderstats:WaitForChild("Rank")
  33.        
  34.         StatsStore:SetAsync(Player.UserId, {
  35.             Points = Points.Value,
  36.             Wins = Wins.Value,
  37.             Rank = Rank.Value,
  38.         })
  39.     end
  40. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement