@daniela.casper
To create a leaderboard for your Roblox game, follow these steps:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
local datastore = game:GetService("DataStoreService"):GetDataStore("Leaderboard") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local leaderboard = Instance.new("IntValue") leaderboard.Name = "Leaderboard" leaderboard.Parent = leaderstats local success, result = pcall(function() return datastore:GetAsync(player.UserId) end) if success and result then leaderboard.Value = result else leaderboard.Value = 0 end leaderboard.Changed:Connect(function(newValue) local success, result = pcall(function() return datastore:SetAsync(player.UserId, newValue) end) if not success then print("Error saving leaderboard value: " .. result) end end) end) |
This code will create a new DataStore2 object to store the leaderboard data, and then create a new "leaderstats" folder for each player that joins the game. It will also create a new "Leaderboard" value in the "leaderstats" folder, which will display the player's current leaderboard score.
When a player joins the game, the script will check if they have a leaderboard score stored in the DataStore2 object. If they do, it will set their leaderboard score to that value. If they don't, it will set their score to 0.
Whenever a player's leaderboard score changes, the script will save the new value to the DataStore2 object.
Now, whenever a player joins your game, they will have a leaderboard score displayed in the "leaderstats" folder. This score will be stored in the DataStore2 object, so it will persist even if the player leaves the game and comes back later.