How do I break a loop in a function when it is a called?

I want to make a loop in a function that can be broke when it is called again. Is there a way to do this?

Basic example:

local function loop()
   for i = 1, 10 do
       print(i)
       
       if i >= 5 then -- Checks if the index value is higher than or equal to 5
          break -- Stops the for loop inside the function from running if the condition was met
       end
   end
end

loop()

I’m talking about if it’s, let’s say, a while loop in the function. If a function is called, it breaks the loop. But thanks!

And I just realized it’s you again haha

What are you trying to achieve? Can you give a code sample?

function waitForBool()
   while wait() do
      if bool.Value == true then
         Int.Value = 1
         break
      end
   end
end

Button.MouseButton1Click:Connect(function()
   if bool.Value == true then
      Int.Value = 1
   elseif bool.Value == false then
      waitForBool()
end)

Do you want to wait until the boolean is true? In that case, you could just use BoolObject.Changed:Wait()

Button.MouseButton1Click:Connect(function()
	if bool.Value == false then
		bool.Changed:Wait()
	end

	Int.Value = 1
end)
2 Likes