Why does my script keep breaking when it doesnt meet the if statement criteria

Basically I have a round system script where it uses a for loop to check if the players are still alive. But for some reason. it completely skips the if #amtOfChildren <= 0 then Part and just completely breaks.

	for i = 300,0, -1 do
		status.Value = "Game: "
		status2.Value = i
		task.wait(1)
		
		if #amtOfChildren <= 0 then
			break
		end
	end

You gave us pretty much nothing to work with and have not defined a solid issue. Please describe more.

2 Likes

My bad.

Basically I have a round system that loops every second and counts down 300 seconds.

There is a folder in workspace that has all the players when a round in progress. And in the loop. It checks the amount of children in folder I mentioned earlier. It uses break to end the round

I mean give us more of the script. What is amtOfChildren? Does it update everytime a children gets removed, maybe that’s the issue?

1 Like

“amtOfChildren” is a folder?
in that case you should use #amtOfChildren:GetChildren()

1 Like

How is amtOfChildren being defined? I believe the source of the issue could lie in that variable.

1 Like

local amtOfChildren = game:GetService("Workspace"):WaitForChild("playersinround"):GetChildren()

And does it update everytime a children get’s removed? If not, do that and try again

2 Likes

It checks every second in the for loop. If thats what you mean

This is a one-time action, you need to do this:

local amtOfChildren = game:GetService("Workspace"):WaitForChild("playersinround")

for i = 300,0, -1 do
		status.Value = "Game: "
		status2.Value = i
		task.wait(1)
		
		if #amtOfChildren:GetChildren() <= 0 then
			break
		end
	end
3 Likes

No, I mean call :GetChildren() on it every on .ChildRemoved

1 Like

Thank you! I made the folder amount update every second

I assume it may be related to the fact you fetch the children once, so the #amtOfChildren value stays constant and doesn’t update. Instead, fetch the children within the loop:

if #something:GetChildren() == 0 then break end
1 Like

I suggest to use .ChildRemoved on the folder and set it there as a loop uses more performance

1 Like

This is what I changed sorry for the confusion:

	for i = 300,0, -1 do
		local amtOfChildren = game:GetService("Workspace"):WaitForChild("playersinround")

		status.Value = "Game: "
		status2.Value = i
		task.wait(1)
		
		if #amtOfChildren:GetChildren() <= 0 then
			break
		end
	end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.