#Folder:GetChildren() not updating when a child is removed

I am trying to create a script that clones a character if the amount of characters is less than 4. Everything works except the count of children stays at 4 when a character is destroyed. For some reason, it works in run test mode but not play test mode for some reason?

I have tried changing the local currentClones from #soldierFolder:GetChildren() to 0 but still didn’t work.

local currentClones = 0
local spawning = true
local maxClones = 4 --max number of clones at one time


while wait(1) do
	currentClones = #soldierFolder:GetChildren()
	print(currentClones)
	
	if currentClones >= maxClones then
		spawning = false
	else
		spawning = true
	end
	
	if spawning then
		print("Cloned")
		local newClone = Soldier:Clone()
		
		newClone.Parent = soldierFolder
		newClone:SetPrimaryPartCFrame(spawnCFrame)
		newClone.Name = Team.Value .. "Soldier"
		newClone.Team.Value = Team.Value
	end
end

Clones spawning in run mode

Deleting it from the folder

^^This is what should happen in play test mode but doesn’t

Any help would be appreciated.

Have you heard of the ChildAdded & ChildRemoved events? :thinking:

local currentClones = 0
local spawning = true
local maxClones = 4 --max number of clones at one time

while wait(1) do
	if currentClones >= maxClones then
		spawning = false
	else
		spawning = true
	end
	
	if spawning then
		print("Cloned")
		local newClone = Soldier:Clone()
		
		newClone.Parent = soldierFolder
		newClone:SetPrimaryPartCFrame(spawnCFrame)
		newClone.Name = Team.Value .. "Soldier"
		newClone.Team.Value = Team.Value
	end
end

soliderFolder.ChildAdded:Connect(function()
	print(currentClones)
	currentClones += 1
end)

soliderFolder.ChildRemoved:Connect(function()
	print(currentClones)
    currentClones -= 1
end)
1 Like

That could work and I had thought of it before, the only problem is that it’s outside of the while loop so it doesn’t function during the loop.

Wait I ordered it wrong

local currentClones = 0
local spawning = true
local maxClones = 4 --max number of clones at one time

soliderFolder.ChildAdded:Connect(function()
	print(currentClones)
	currentClones += 1
end)

soliderFolder.ChildRemoved:Connect(function()
	print(currentClones)
    currentClones -= 1
end)

while wait(1) do
	if currentClones >= maxClones then
		spawning = false
	else
		spawning = true
	end
	
	if spawning then
		print("Cloned")
		local newClone = Soldier:Clone()
		
		newClone.Parent = soldierFolder
		newClone:SetPrimaryPartCFrame(spawnCFrame)
		newClone.Name = Team.Value .. "Soldier"
		newClone.Team.Value = Team.Value
	end
end

They should go first before you start the while wait(1) do loop

I tried recreating your script in studio and didn’t run into any problems.

First off, instead of using a while true do loop, try using events such as ChildAdded, and ChildRemoved.

Also if this script is a serverscript, deleting the models on the client will not replicate to the server.

1 Like

It loads in but has the exact same problem as before

Oh, I see. I guess that explains why it didn’t work. Would it work if the child gets deleted from a script?

You can get amount of childrens also like this:

local count = 0
local Folder = workspace.Folder
for i, v in pairs(Folder:GetChildren()) do
count += 1
end
print(count) 

Just changed it and it works perfectly, thank you.