I am attempting to create a custom leaderboard for my game, but the script to add players to it is not running, while also returning no errors. Is this some scripting rule I know nothing about?
script: (local)
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.PlayerList, false)
local order = -50
local players = game:GetService("Players")
for i, v in pairs(game.Teams:GetChildren()) do
local template = script.Parent.TeamTemplate:Clone()
template.Parent = script.Parent
template.Text = v.Name
template.Name = v.Name
template.LayoutOrder = order + 50
template.BorderColor3 = v.TeamColor.Color
order += 50
end
script.Parent.TeamTemplate:Destroy()
game.Players.PlayerAdded:Connect(function(player)
local template = script.Parent.PlayerTemplate:clone()
template.Parent = script.Parent
template.Text = player.Name
template.Name = player.Name
for i, textbox in pairs(game.StarterGui.ScreenGui.Leaderboard:GetChildren()) do
if textbox.ClassName == "TextLabel" and textbox.BorderColor3 == player.TeamColor.Color then
template.LayoutOrder = textbox.LayoutOrder + 1
end
end
end)
for i, textbox in pairs(game.StarterGui.ScreenGui.Leaderboard:GetChildren()) do
Don’t use game.StarterGui, when in-game, it ‘becomes’ PlayerGui, so you won’t see effects when trying to access StarterGui like that. [First line is okay and fine, but not when trying to change player’s stuff such as a textButton,etc]
Now, game.Players.PlayerAdded:Connect(function(player) runs and has an effect only when called on server script.
The number one rule of good practice working with added events is also to handle existing instances with the matching getter method. This will become especially important once deferred mode becomes the standard of programming with events on Roblox.
local function playerAdded(player)
-- Your code here
end
Players.PlayerAdded:Connect(playerAdded)
for _, player in ipairs(Players:GetPlayers()) do
playerAdded(player)
end
You should probably debug your code more because I don’t think you’re actually certain this code does not run. PlayerAdded can be connected to via the client meaning that if it’s not firing then whoever you’re trying to check in added joined before the connection is established. This is especially true for the LocalPlayer who will connect before client-side code starts executing.
Underscore is used to denote an unused variable. In my case, I don’t ever use the index of the ipairs generator so I use an underscore to make it clear that the index variable isn’t used in code. It prevents any potential shadowing with other variables as well. No special meaning otherwise.
ipairs will iterate through numerically contiguous arrays at i++ (tbl[1] … tbl[n]) while pairs will iterate using the next function. ipairs is best for working with arrays while pairs for dictionaries that have an arbitrary ordering. These are things you can search up on your own time.