Why is my leaderboard script not working (SOLVED)

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:
thing
Thanks.

3 Likes

I’m pretty sure PlayerAdded and PlayerRemoving can only be used in a server script.

4 Likes

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.

2 Likes

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.

2 Likes

Thanks, I’ll try it out right now.

1 Like

Alright The Script now works under starter player scripts but I’m getting this error:

IsPlayerLoaded is not a valid member of Player "Players.PuffyJasonrocks84"
1 Like

replace it with game:IsLoaded()

1 Like

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)
2 Likes

This code snippet works but I’m getting a new error here:

Text is not a valid member of UIListLayout "Workspace.SpawnAreaModel.LeaderBoard.SignPart.SurfaceGui.ScrollingFrame.UIListLayout"

It also does not remove the players Label once they leave
Thanks.

1 Like

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
2 Likes

This script now works perfectly thank you so much for your help.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.