Event handling query

Hi,
I have an EventHandling script as shown below. The inmenu successfully is created and destroyed, however if a player has a “Main” tag, it does not destroy the Lobby tag I created in the player. Note, the tag for “Main” is created in a sibling script. (So maybe this has something to do with it and this script doesn’t recognise the Main tag?)
Can anyone see where Ive gone wrong?
Many thanks

game.Players.PlayerAdded:Connect(function(player)

	
	
	
	local inMenu = Instance.new("BoolValue")
	inMenu.Name = "InMenu"
	inMenu.Parent = player
	
	local Lobby = Instance.new("StringValue")
	Lobby.Name = "Lobby"
	Lobby.Parent = player
	


end)

game.ReplicatedStorage.MenuPlay.OnServerEvent:Connect(function(player)
	if player:FindFirstChild("InMenu") then
		player.InMenu:Destroy()
	end
end)


for i, player in pairs(game.Players:GetPlayers()) do
	if player:FindFirstChild("Main") then
		player.Lobby:Destroy()
	end
end

This could be because the “Main” tag is generated AFTER the loop checks for “Main” in all players.

Depending on the purpose of “Main,” may consider adding a wait before looping through the players?

Yes I believe the ‘Main’ tag will be generated after as it is created when a player changes teams 10s ish into the game.
I added a long wait above the final for loop, but this didnt seem to fix it.

In full, the purpose of this is to help my spectate script realise when a player has changed team.
So alternatively, in my spectate script I have this for loop

for i, player in pairs(game.Players:GetPlayers()) do
	if player:FindFirstChild("Lobby") then
		button.Visible = true
		frame.Visible = true

	else
		button.Visible = false
		frame.Visible = false

	end
end

It works to make the button & frame visible for the Lobby team, however when the player is actually removed from this team, the button & frame stays and doesn’t actually dissapear. Any ideas?

Yes! I believe what you can do is use a ChildAdded and ChildRemoved connection. These events basically listen for whether a child is added or removed on an Instance.

1 Like