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
Have you heard of the ChildAdded & ChildRemoved events?
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)
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