Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- leadersats for https://pastebin.com/TpfrcDCq
- -- credit: cat_givemyrobox
- local DataStoreService = game:GetService("DataStoreService")
- local pointsDataStore = DataStoreService:GetDataStore("PlayerPointsDataStore")
- -- Function to create leaderstats for a player
- local function setupLeaderstats(player)
- local leaderstats = Instance.new("Folder")
- leaderstats.Name = "leaderstats"
- leaderstats.Parent = player
- local points = Instance.new("IntValue")
- points.Name = "Points"
- points.Parent = leaderstats
- -- Attempt to load the player's points from the DataStore
- local success, data = pcall(function()
- return pointsDataStore:GetAsync(player.UserId)
- end)
- if success and data then
- points.Value = data
- else
- points.Value = 0 -- Default value if no data exists
- end
- end
- -- Save points data when the player leaves the game
- local function savePlayerPoints(player)
- local points = player.leaderstats and player.leaderstats.Points
- if points then
- local success, error = pcall(function()
- pointsDataStore:SetAsync(player.UserId, points.Value)
- end)
- if not success then
- warn("Failed to save points for " .. player.Name .. ": " .. error)
- end
- end
- end
- -- Connect the functions to Player events
- game.Players.PlayerAdded:Connect(function(player)
- setupLeaderstats(player)
- end)
- game.Players.PlayerRemoving:Connect(function(player)
- savePlayerPoints(player)
- end)
- -- Setup leaderstats for players already in the game
- for _, player in pairs(game.Players:GetPlayers()) do
- setupLeaderstats(player)
- end
- return setupLeaderstats
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement