ChildAdded working but not ChildRemoved?

I am making a lobby system that needs to refresh characters when a change occurs. Currently, the characters are supposed to refresh if a child is added to or removed from a folder. For some reason, ChildAdded is working correctly but not ChildRemoved.

The code (the problem happens at the very bottom):

function JoinLobbyVisualizer(RoomHost) --roomhost is the player instance of the host
	HideAll() --hides all other frames in the main menu
	LobbyHud.Visible = true
	Camera.CFrame = workspace.LobbyMenuCamera.CFrame
	local PlayerModels = ReplicatedStorage.PlayerModelsRoom:Clone()
	PlayerModels.Parent = workspace
	local P1Hum = PlayerModels:WaitForChild("Player1"):WaitForChild("Humanoid")
	local P2Hum = PlayerModels:WaitForChild("Player2"):WaitForChild("Humanoid")
	local P3Hum = PlayerModels:WaitForChild("Player3"):WaitForChild("Humanoid")
	local P4Hum = PlayerModels:WaitForChild("Player4"):WaitForChild("Humanoid")
	local Room = Lobbies:FindFirstChild(RoomHost.Name) --get current room (the room folder is named the same as the host's name)
	if Room == nil then
		warn("The room is nil!") --for debugging just in case
	end
	local CurrRoomPlayers = Room:WaitForChild("Players") --a folder with all of the players in objectvalues that are in this lobby
	local PlayersStepped = 0
	local function RefreshPlayerModels()
		task.wait(0.2)
		print("Refreshing player models...")
		P1Hum:ApplyDescriptionReset(script.DefaultDescription)
		P2Hum:ApplyDescriptionReset(script.DefaultDescription)
		P3Hum:ApplyDescriptionReset(script.DefaultDescription)
		P4Hum:ApplyDescriptionReset(script.DefaultDescription)
		for i, v in pairs(CurrRoomPlayers:GetChildren()) do
			local CurrUserId = v.Value.UserId
			PlayersStepped += 1
			if PlayersStepped == 1 then
				P1Hum:ApplyDescriptionReset(game.Players:GetHumanoidDescriptionFromUserId(CurrUserId))
			elseif PlayersStepped == 2 then
				P2Hum:ApplyDescriptionReset(game.Players:GetHumanoidDescriptionFromUserId(CurrUserId))
			elseif PlayersStepped == 3 then
				P3Hum:ApplyDescriptionReset(game.Players:GetHumanoidDescriptionFromUserId(CurrUserId))
			elseif PlayersStepped == 4 then
				P4Hum:ApplyDescriptionReset(game.Players:GetHumanoidDescriptionFromUserId(CurrUserId))
			else
				warn("There was more players in this lobby than the system was expecting!")
			end
		end
		PlayersStepped = 0
	end
	RefreshPlayerModels() --we call this immediately so the host will get loaded

	--THIS IS WHERE THE PROBLEM OCCURS:
	CurrRoomPlayers.ChildAdded:Connect(RefreshPlayerModels)
	CurrRoomPlayers.ChildRemoved:Connect(RefreshPlayerModels)
end

Any explanation as to why this isn’t working correctly would be much appreciated!

Does the “Refreshing player models…” get printed at all when the room is left / Does the function work at all…?

No, it only gets printed when it is joined. The function does work.

If there is some way, Try putting fake characters in there. And move the ChildRemoved above the ChildAdded and then remove one of the characters from the room and see if it updates. I’m not sure why it’s doing it tbh, I’ve never heard of where you can’t have Added and Removed events on the same Object. I just want you to do this to see if it’s just because of where it’s at.

I tried creating temporary values and deleting them from the server, and it did indeed work. It seems like the problem may be that players don’t get correctly deleted when they leave a lobby, but I don’t see what that problem is. This is the server code for leaving a lobby:

function LeaveLobbyRequest(Player)
	print(Player.Name .. " is requesting to leave their heist")
	local RequestedLobby = nil
	for i, v in pairs(Lobbies:GetChildren()) do
		local SearchHost = v.Host
		if SearchHost.Value.Name == Player.Name then
			print("Match found!")
			RequestedLobby = v
		end
	end
	if RequestedLobby == nil then
		print(Player.Name .. " is not the host! Continuing search...")
		local RequestedLobby = nil
		for i, v in pairs(Lobbies:GetChildren()) do
			local CurrPlayers = v.Players
			for i, v2 in pairs(CurrPlayers:GetChildren()) do
				if v2 == Player then
					v2:Destroy()
					local Action = "Leave"
					local Reason = ("The lobby host cancelled the heist!")
					local RoomHost = v.Host
					game.ReplicatedStorage.LobbySystem.ReplyLobbyJoinLeave:FireClient(Player, Action, Reason, RoomHost)
				end
			end
		end
	else
		RemoveLobby(Player)
	end
end

For this, did you swap the places of the (Child) functions?

Yes, it did not change anything.

For this, does the value actually get destroyed?

1 Like

Whoops, I just realized my mistake. I was doing v2 == Player instead of v2.Value == Player, which causes the value to never be found. Thanks for your help!

1 Like