I am making a survival-type game and there is a leaderboard in the game lobby that displays the wins of the players in the same server but I am having trouble cloning the Text Label into the scrolling frame using a local script here is the code:
local surfaceGui = script.Parent:WaitForChild('SurfaceGui')
local scrollingFrame = surfaceGui.ScrollingFrame
local sample = script:WaitForChild('Example')
local plr = game.Players.LocalPlayer
print("TEST")
local function addLabel(player)
print("Working?")
local label = sample:Clone()
label.Text = player.Name
label.Parent = scrollingFrame
end
local function removeLabel(player)
print("Working??")
for _, label in ipairs(scrollingFrame:GetChildren()) do
if label.Text == player.Name then
label:Destroy()
break
end
end
end
if plr:IsPlayerLoaded() then
addLabel(plr)
end
plr.PlayerAdded:Connect(addLabel)
plr.PlayerRemoving:Connect(removeLabel)
I put a test print in the beginning of the script but it’s not firing and I don’t know why here is an img of the workspace:
This is not true, they work fine in a LocalScript, but just not for the client they’re running on because PlayerAdded has already fired by the time the client’s event listener is declared and connected.
For your issue, assuming your Leaderboard is under the workspace, driven by a LocalScript, LocalScripts do not run as a descendant to anything under the workspace with an exception of being parented to the player’s character. The code itself may very well be just fine, but you’ll instead need to parent it to a local service such as StarterPlayerScripts in order to yield the local outcome you’re desiring.
There’s no such function as IsPlayerLoaded of the Player object that you seem to be trying to use. Once the client has joined and the LocalScript has ran, you can safely assume that the player has “loaded”. If not, you would’ve already received an error while referencing your LocalPlayer.
Additionally, PlayerAdded is an event of the Players service, not the player object.
Give the below code a shot and report back on any issues you may still encounter.
local Players = game:GetService("Players")
local surfaceGui = script.Parent:WaitForChild('SurfaceGui')
local scrollingFrame = surfaceGui.ScrollingFrame
local sample = script:WaitForChild('Example')
local plr = game.Players.LocalPlayer
print("TEST")
local function addLabel(player)
print("Working?")
local label = sample:Clone()
label.Text = player.Name
label.Parent = scrollingFrame
end
local function removeLabel(player)
print("Working??")
for _, label in ipairs(scrollingFrame:GetChildren()) do
if label.Text == player.Name then
label:Destroy()
break
end
end
end
if plr then
addLabel(plr)
end
Players.PlayerAdded:Connect(addLabel)
Players.PlayerRemoving:Connect(removeLabel)
That is because your removeLabel function’s for loop is getting caught in the error in an iteration by checking for the Text property of a UIListLayout object you may have parented for listing, which doesn’t exist.
You can simply fix this by checking if the object in said iteration is a TextLabel before checking or comparing any of its properties (presuming it’s a TextLabel).
Try replacing your removeLabel function with this.
local function removeLabel(player)
print("Working??")
for _, label in ipairs(scrollingFrame:GetChildren()) do
if label:IsA("TextLabel") and label.Text == player.Name then
label:Destroy()
break
end
end
end