How can i stop loop in this script

I want to create a function that runs once when the distance between the player character and the floor is > 50. The problem is that it doesn’t stop after the first run.

All methods i tried:

if runned == true then return end

repeat

until loops = 1

but thats not work.

There is code:

while true do
		for i,v in pairs(game.Players:GetChildren()) do
			if v:IsA("Player") then
				local Char = v.Character or v.CharacterAdded:Wait()
				local HRP = Char:WaitForChild("HumanoidRootPart")
				
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Include
				params.FilterDescendantsInstances = {workspace.Map}
					
				local ray = workspace:Raycast(HRP.Position,Vector3.new(0,-99999,0),params)
				
				if ray then
					if ray.Distance < 50 then
						--there is rocks spawning
					end
				end
			end
		end
		wait()
	end

How it actually work:

How it must work:

I need to run the function once as soon as the Player character reaches distance < 50, so that it doesn’t run the function again

local runs = 1
repeat
     -- Your main loop here --
     runs += 1
until runs >= 5

This loop is always running, unless you have it inside another function.
Instead of print("Done") as your check use print(ray.Distance) so you can find out the actual distance.

The other thing is try this

if ray.Distance < 50
    print(ray.Distance)
    local ray = workspace:Raycast(HRP.Position,Vector3.new(0,-99999,0),params)
end

Or
while ray.Distance < 50 do
for your main loop instead of while true do

1 Like

U should use “break” to stop any kind of loop, this includes while loops,

Example:
while true do
if runned == true then break end
wait()
end

Additional information:
“return” would also work, butbonly if you did put the statement right at the start of the loop, HOWEVER this would cause the loop to possibly freeze up/ break because the loop will just stop there, if the statement is true and if there is no yielding.

So please use “break” instead of “return” to stop any kind of loop.
Hope this helped :smile:

1 Like

You can stop a loop by using break:

while true do
break end

Edit: Someone beat me to it

Make a variable that acts as a limiter like what @Blankscarface23 showed you, then use break instead of until since it will stop the whole entire loop.

-- repeat
local LoopLimit = 10
local runs = 1

repeat
    -- Main Loop
    runs = runs + 1
if runs = LoopLimit then
    break
end

-- while true do
local Limit = 5
local runs = 1

while true do
      -- Main Loop
      runs = runs + 1
      if runs == Limit then
             break
       end
end

This is how I would do the whole stopping loop thingy.

Yes, the lua script looks rancid but should help.

I need to stop the loop function

not the whole script

I’ve sent you an example.
You may be free to customize it however you like.

This should work, but using break is not recommended for best practices.
Hence, I used a variable to limit the loop execution.

If I had more context about how this loop is being used, I might have used RenderStepped.