Advertisement
SimTek

GameMgr

May 11th, 2024
1,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("MyData")
  3.  
  4. local function addPlayerData(player)
  5.     local board = Instance.new("Folder", player)
  6.     board.Name = "leaderstats"
  7.  
  8.     local kills = Instance.new("IntValue", board)
  9.     kills.Name = "Kills"
  10.  
  11.     local assists = Instance.new("IntValue", board)
  12.     assists.Name = "Assists"
  13.  
  14.     local deaths = Instance.new("IntValue", board)
  15.     deaths.Name = "Deaths"
  16.    
  17.     local pData = Instance.new("Folder", player)
  18.     pData.Name = "playerdata"  
  19.    
  20.     local points = Instance.new("IntValue", pData)
  21.     points.Name = "Points"
  22.    
  23.     local totKills = Instance.new("IntValue", pData)
  24.     totKills.Name = "TotKills"
  25.  
  26.     local totAssists = Instance.new("IntValue", pData)
  27.     totAssists.Name = "TotAssists"
  28.  
  29.     local totDeaths = Instance.new("IntValue", pData)
  30.     totDeaths.Name = "TotDeaths"
  31. end
  32.  
  33. local function initPlayerData(player)
  34.     local data = ds:GetAsync(player.UserId)
  35.     print("saved data = ", data)
  36.     if data then
  37.         player.playerdata.TotKills.Value = data.TotKills
  38.         player.playerdata.TotAssists.Value = data.TotAssists
  39.         player.playerdata.TotDeaths.Value = data.TotDeaths
  40.         player.playerdata.Points.Value = data.Points
  41.     end
  42. end
  43.  
  44. local function savePlayerData(player)
  45.     local data = {}
  46.     data.TotKills = player.playerdata.TotKills.Value +
  47.         player.leaderstats.Kills.Value
  48.     data.TotAssists = player.playerdata.TotAssists.Value +
  49.         player.leaderstats.Assists.Value
  50.     data.TotDeaths = player.playerdata.TotDeaths.Value +
  51.         player.leaderstats.Deaths.Value
  52.     data.Points = player.playerdata.Points.Value
  53.     print("data = ", data)
  54.     ds:SetAsync(player.UserId, data)
  55. end
  56.  
  57. game.Players.PlayerAdded:Connect(function(player)
  58.     addPlayerData(player)
  59.    
  60.     player.CharacterAdded:Connect(function(char)
  61.         local hum = char:WaitForChild("Humanoid")
  62.         hum.Died:Connect(function()
  63.             player.leaderstats.Deaths.Value += 1
  64.             local tags = hum:GetChildren()
  65.             for i, v in pairs(tags) do
  66.                 if v.Name == "creator" then
  67.                     local ePlayer = v.Value
  68.                     local kills = ePlayer.leaderstats.Kills
  69.                     kills.Value += 1
  70.                 end
  71.                 if v.Name == "assist" then
  72.                     local ePlayer = v.Value
  73.                     local assists = ePlayer.leaderstats.Assists
  74.                     assists.Value += 1
  75.                 end        
  76.             end
  77.         end)
  78.     end)
  79.     initPlayerData(player)
  80. end)
  81.  
  82. game.Players.PlayerRemoving:Connect(function(player)
  83.     savePlayerData(player)
  84. end)
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement