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!