Script to delete a frame when player leaves not working

Hey devs,
I am currently working a lobby system to teleport players to the main game. Everything worked until now appart from that : I have a list of all the players in the lobby, and I want to remove a player from the list if he leaves, thats what it tried so far, i don’t really understand what’s the issue…

--LocalScript in StarterGui
local frame1 = script.Parent.Lobby1

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr)
	for i, v in pairs(frame1:GetChildren()) do
		if v:IsA('Frame') then
			print(v.Name)
			if v.Name == plr then
				v:Destroy()
			end
		end
	end
end)

Hope someone can help !

Your code works perfectly, but your code does not line up with your intentions.

This is what we call an error in logic, and this is something that doesn’t actually break your script during runtime (which is why it can be difficult to diagnose, in some cases). This script is doing exactly what you’re telling it to do.

You are checking if a string is equal to an instance, which would never equate to true.

Here is your check:

image

To achieve the wanted result, this should be changed to:

if v.Name == plr.Name then
1 Like

Thank you so much ! What a stupid error, I should have noticed haha

Not at all! These things slip every coder’s mind at some point, what counts is your ability to debug and search for errors such as these. :slight_smile:

Enjoy and good luck with your project.

2 Likes

Well it’s something unrelated but instead of looping through all of the children of the frame, you could just directly find it (given it exists) since the child’s name is the player’s name.

frame1[plr.Name]:Destroy()
1 Like