Advertisement
Sufferrrrrr

Leaderstats for https://pastebin.com/TpfrcDCq

Jan 4th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. -- leadersats for https://pastebin.com/TpfrcDCq
  2. -- credit: cat_givemyrobox
  3.  
  4. local DataStoreService = game:GetService("DataStoreService")
  5. local pointsDataStore = DataStoreService:GetDataStore("PlayerPointsDataStore")
  6.  
  7. -- Function to create leaderstats for a player
  8. local function setupLeaderstats(player)
  9.     local leaderstats = Instance.new("Folder")
  10.     leaderstats.Name = "leaderstats"
  11.     leaderstats.Parent = player
  12.  
  13.     local points = Instance.new("IntValue")
  14.     points.Name = "Points"
  15.     points.Parent = leaderstats
  16.  
  17.     -- Attempt to load the player's points from the DataStore
  18.     local success, data = pcall(function()
  19.         return pointsDataStore:GetAsync(player.UserId)
  20.     end)
  21.  
  22.     if success and data then
  23.         points.Value = data
  24.     else
  25.         points.Value = 0 -- Default value if no data exists
  26.     end
  27. end
  28.  
  29. -- Save points data when the player leaves the game
  30. local function savePlayerPoints(player)
  31.     local points = player.leaderstats and player.leaderstats.Points
  32.     if points then
  33.         local success, error = pcall(function()
  34.             pointsDataStore:SetAsync(player.UserId, points.Value)
  35.         end)
  36.  
  37.         if not success then
  38.             warn("Failed to save points for " .. player.Name .. ": " .. error)
  39.         end
  40.     end
  41. end
  42.  
  43. -- Connect the functions to Player events
  44. game.Players.PlayerAdded:Connect(function(player)
  45.     setupLeaderstats(player)
  46. end)
  47.  
  48. game.Players.PlayerRemoving:Connect(function(player)
  49.     savePlayerPoints(player)
  50. end)
  51.  
  52. -- Setup leaderstats for players already in the game
  53. for _, player in pairs(game.Players:GetPlayers()) do
  54.     setupLeaderstats(player)
  55. end
  56.  
  57. return setupLeaderstats
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement