While loop never starts

I am trying to make a zone, that could be captured by other players. Whenever a player touches/stops touching the zone, he is added/removed from the touchingPlrs table. This works fine.

Then i wrote

while #touchingPlrs > 0 do
    print("zone is being captured")
end

However, the loop never starts.

Any idea why?

Did you try printing out the value? We need more context.

You need to show how you’re population touchinhPlrs

As well as when you start running this loop.

IftouchinhPlrs is empty when you try to call the loop, it will never run.

I would at least suggest changing this into a Heartbeat call from RunService, then use your current logic in the loop as an if statement within Heartbeat to do what you need.

That way it is always running.

Yes, I did. I was printing #touchingPlrs every second. If the player was touching the zone, it printed 1, whenever the player wasnt touching, it printed 0.

Whenever the player touches the zone, it fires .Touched event, and the player(not the character) is added to the table. Whenever the player stops touching the zone, it fires .TouchEnded event and the player is removed.

That’s great and all, but as mentioned if you run the while loop before any touchingPlrs are added, the loop will never run, because you set the logic to only run if the list if at least one player is in it.

It will not start itself automatically. Hence why I said change it to use

game:GetService(“RunService”).Heartbeat:Connect(function(dt)
   if (#touchingPlrs > 0) then
      
   end
end)

… instead

1 Like

Alright, I’ll try that. Thank you!