What is wrong with my Leaderboard Script?

Hello,

So I’m Making a Custom Leaderboard, I want it to Delete the UI (if there is any), and then Create a New TextLabel for the Players found.


ISSUE?

The Script works, but when i attempt to Delete the TextLabels, It deletes them but the for loop Below doesn’t work? What is Happening?


MainPlr is the TextLabel that Applies to the Player its Running for
C is the TextLabel for other Players

Player = game.Players.LocalPlayer -- Player
PlayerGui = Player:WaitForChild("PlayerGui") -- Player Gui

PlrLabel = game.ReplicatedStorage:WaitForChild("PlrLabel") -- Template
Frame = script.Parent.Frame -- Main Frame


Frame.MainPlr.Text = "  "..tostring(Player) -- Applies Main Label

function UpdateUI()
	for _,i in pairs(Frame:GetChildren()) do
		if i.Name ~= "MainPlr" then -- If Name is not Equal to MainPlr
			i:Destroy() -- Deletes UI that isnt MainPlr
		end
		
	end

	for _,p in pairs(game.Players:GetChildren()) do
		if p ~= Player then -- If Player isnt the Player its running for
			local C = PlrLabel:Clone() -- New UI
			C.Parent = script.Parent.Frame -- Parents to Leaderboard
				
			C.Text = "  "..tostring(p) -- Players Name
		end
	end
	print("Updated!") -- Test
	
end

WHAT I WANT:

RobloxScreenShot20221222_214550148

WHAT HAPPENS:

RobloxScreenShot20221222_214659094

I Made sure to Test them with Multiple Players.


When Testing, Only MainPlr is Updated, while nothing happens in the function. Why so?

Thanks.

Trying using :GetPlayers() instead?

1 Like

Both of them work the same way here, but is the Deleting Part that I’m having the Trouble with.

Thanks tho! :slight_smile:

Which event are you calling the UpdateUI function in? You should be making separate functions for the PlayerAdded event and PlayerRemoving event if these are the events you are using.

1 Like

Im Calling it every 10 seconds via while loop


I’m not using any Events, just a Basic Leaderboard Script


Like I said, It works, but the deleting part is the issue

Instead maybe try:

game.Players.ChildRemoving:Connect(function(leavingplayer)
  for _,i in pairs(Frame:GetChildren()) do
     if i.Name == leavingplayer.Name then -- If Name is not Equal to MainPlr
		i:Destroy() -- Deletes UI that isnt MainPlr
	end	
end
end)
1 Like

I believe using a while loop is an inefficient way of achieving this. You should instead use the PlayerAdded and PlayerRemoving events. Try creating separate functions for both of the events (one function for adding a UI when a player joins, and another function for deleting the UI when a player leaves).

Here is an example of what your code should look like:

Player = game.Players.LocalPlayer -- Player
PlayerGui = Player:WaitForChild("PlayerGui") -- Player Gui

PlrLabel = game.ReplicatedStorage:WaitForChild("PlrLabel") -- Template
Frame = script.Parent.Frame -- Main Frame


Frame.MainPlr.Text = "  "..tostring(Player) -- Applies Main Label

function PlayerList(player)
	local C = PlrLabel:Clone()
	C.Parent = script.Parent.Frame
	C.Text = "  "..tostring(player)
end

function PlayerAddedFunc(plr)
	PlayerList(plr)
end

for _, v in pairs(game.Players:GetPlayers()) do
	PlayerList(v)
end

function PlayerRemoving(plr)
	for i, v in pairs(script.Parent.Frame:GetChildren()) do
		if v and v:IsA("TextButton") and v.Text == plr.Name then -- I am not sure what your UI Class is. If it's not a TextButton, change it to its exact ClassName
			v:Remove()
		end
	end
end

game.Players.PlayerAdded:Connect(PlayerAddedFunc)
game.Players.PlayerRemoving:Connect(PlayerRemoving)
2 Likes

Appearently I’m a Idiot,

It turns out I was accidentally deleting the UiListLayout keeping the Leaderboard together.

Code that worked:

for _,i in pairs(Frame:GetChildren()) do
		if i:IsA("TextLabel") then
			if i.Name ~= "MainPlr" then
				i:Destroy()
			end	
		end
	end

Thank you both @BabyNinjaTime and @coolifysz for trying to help!

I get that, but that’s not what I’m trying to do, Thanks for helping.

1 Like

Actually, you deserve the Solution more. Thanks!

1 Like

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