How to make a loop that never ends?

Hello devs!

I am having some difficulties creating an “invincible” loop meaning that when the loop runs into an error it does not break the loop. Ideally a loop that when it runs into an error it restarts the loop.

For example:

while true do

wait(10)

for i, player in ipairs(game.Players:GetChildren()) do
	local character = player.Character or player.CharacterAdded:wait()
	character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
end 

end

This loop gets all players and teleports them. If this loop runs into an error getting one of the players the entire loop can just stop running for the remaining life of the server. How can we change this code to give it some sort of back-up measure that if it runs into an error the loop just restarts and tries again.

Thank you so much in advance!

1 Like

… Why are you pcalling it? There’s no reason that should error. You don’t even handle an error in the case there is one, which is even worse, since you won’t be able to debug.

2 Likes

I definitely do want to debug…

The way it was set up when you first wrote it would it not have showed an error on console? I think I prefer the first variation of code you had. I believe it will still error on the console, please correct if wrong.

Alright going to run some tests.

Pls, not use a while wait() loop, this is a very bad practice

Are you serious? You should really use this method here instead of the other one:

1 Like

Use :Wait() instead of :wait() and pcall is unneeded.

1 Like

Should it be:

while true do
wait(10
–code
end

Is that better practice?

1 Like

Yes, and please don’t add pcall in the loop.

There is absolutely no need for a pcall here. pcall is used for when an exception is out of your control, like making a web request. You can’t guarantee that the web request will be a success so that is where pcall comes in. Any exceptions thrown here are through fault of the OP which are in control of the OP.

2 Likes

Yep, there are three ways to check HumanoidRootPart.

  1. PrimaryPart
  2. FindFirstChild
  3. Humanoid.RootPart

I did not make it very clear but that is just an example code. Imagine a much more complex loop that is running many codes. I think the pcall is actually necessary.

I am not talking about the pcall, but about the game.Players:GetChildren() method:

1 Like

I agree with you on this, I changed it to that now.

1 Like

Works well in my tests, thank you.

Why not simply pcall the teleportation?


while true do
	wait(10)
	for i, player in ipairs(game.Players:GetChildren()) do
		local character = player.Character or player.CharacterAdded:wait()
		character.HumanoidRootPart.CFrame = CFrame.new(0, 0, 0)
	end
end

Pls use the Lua Syntax (i mean that you need to correctly use the ```). It will help other users to read your code!

There are many loops, but no loop is more infinite than the inifinite loop!

Btw you might find coroutines helpful; such that you spawn a new coroutine to handle each players individual teleport. Pcall is specifically for handling errors, coroutines isn’t a substitute.

But if you end up making more complex scripts where the code ur looping might yield, then this implementation will keep the loops iterations constant in time. And any behavior in a coroutine doesn’t affect the control flow of the one that started it. (including errors)

One more thing; don’t try to use pcalls to make an invincible loop, try and understand the code and its behavior and ensure that it’ll never error. Because you don’t want to wrap everything with pcall.

3 Likes

I 100% agree that one should fix the actual errors and not put a bandaid on it. Though it is still good to have a back up measure just in case you do run into an error a vital game mechanic does not break. Also i will look into coroutines, thank you!!

2 Likes