I have a system implemented in my game where the default leaderboard is replaced with a custom GUI, where:
A player joins / leaves the game
A GUI stored in ReplicatedStorage is edited to append / remove a TextLabel with the player’s name
A remote event is then fired to ask clients to update the leaderboard (server → client)
The GUI is cloned by the client into the player’s PlayerGui
I’m wondering primarily whether this is the most optimised way to handle implementing my own, custom leaderboard, or if there might be a service (/ traditional way) to achieve the same goal.
Until this comment, I had never realised that you could call game.Players.PlayerAdded on the client!
Thank you for what is, quite literally, a revelation!
(Thank you also for the tip on UIListLayout, I hadn’t thought of using that whilst designing!)
Edit: I’ve decided to stick with RemoteEvents, due to the inconsistency of connecting Players.PlayerAdded to a function in solo mode, as the player joins before the script executes.
Unfortunately, you can’t simply hook the PlayerAdded event to a function when playing in singleplayer, as the LocalScript runs after the player has joined, meaning that the player isn’t added to the leaderboard.
I did try something similar:
local function onPlayerAdded(plyr)
print(plyr.Name)
end
game:GetService("Players").PlayerAdded:Connect(onPlayerAdded)
for k, v in pairs(game:GetService("Players"):GetChildren()) do
onPlayerAdded(v)
end
Unfortunately, though, this causes things to iterate twice, which isn’t particularly helpful when working with a singular GUI.
Not sure what you mean. It only prints once. Regular LocalScript in StarterPlayerScripts.
local Players = game:GetService("Players")
local function playerAdded(player: Player)
print(player)
end
Players.PlayerAdded:Connect(playerAdded)
for _, player in Players:GetPlayers() do
playerAdded(player)
end