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:
WHAT HAPPENS:
I Made sure to Test them with Multiple Players.
When Testing, Only MainPlr is Updated, while nothing happens in the function. Why so?
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.
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)
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)