How do I break a loop in one script from another?

I want to break a while loop in one script from another script.

I don’t know how to do it.

I was thinking of adding a condition in a loop, checking a variable that if true, will break the loop from the inside scope, so I could then change that variable from another script.

But that’s busy-waiting and I want it to be more event-based, so like, a function that I call that breaks the loop, maybe?

But a function outside of a loop can’t break the loop even if it is in the same script, which is why I’m stuck.

1 Like

You could make a bool value and change the value to break the loop.

2 Likes

You could maybe do this with a variable, also I don’t think using a BoolValue would use too much resources. As function names also count as variables, you can just set it to nil and use an if statement to check if the function still exists.

local function Loop()
	print("Hi")
end

task.delay(5, function() -- Instead of delaying use your event
	Loop = nil
end)

while task.wait(1) do
	if Loop then
		Loop()
	else
		break
	end
end
1 Like

Break from another script*

talking about _G. i guess

Example:

_G.BreakMode = false -- BoolValue

local function A()

     repeat 
          --repeat what
          if BreakMode == false and SomeLogicHere then
            _G.BreakMode = true
              break -- if you want to break this too
           end
   until

   -- until what

end

In another script:

local function B()

        repeat
           --repeating something
            if _G.BreakMode == true then 
            print"Function A said to break myself"
            break

        until

---Something running nonstop or whatever

end

just an exampl,e, maybe there is some other way to do that

You can use bindable events, which can trigger it easily with Event:Fire

--//Script with loop
local running = true
local break_loop = Instance.new('BindableEvent') do --//"do" is for organizational purposes
    break_loop.Name = 'BreakLoop'
    break_loop.Parent = script
    break_loop.Event = function()
        running = false
    end
end

while running and task.wait() do --//Checks if "running" is true, and waits about 1/40 seconds
    --//loopy
end
--//Script without loop

local script = game.ServerScriptService:FindFirstChild('LoopyScript') --//Script with loop in it
local break_loop = script:WaitForChild('BreakLoop')


break_loop:Fire() --//Breaking the loop

Make sure to name the event something specific when you make a new one, so you don’t have a ton of “BreakLoop” events in one instance, because that would sure be confusing!

1 Like

Talking about ModuleScripts, not _G

It’s still busy waiting though, just instead of running being changed directly, you call an event that changes it.

You can use a table, return it from the modulescript carrying data saying that its still running, and if you change the data the loop will stop:

--//Modulescript
local module = {}
module.loop_running = true

while true do
    if (module.loop_running == false) then
        break
    end
end
--//other script, module/regular
local loopermodule = require(modulescript)

loopermodule.loop_running = false --//stops the loop

Or you can use Heartbeat that runs 40 times a second, and disconnect it:

--//ModuleScript
local module = {}
module.loop = game:GetService('RunService').Heartbeat:Connect(function()
    --//Runs 40 times a second
end)
local loopermodule = require(modulescript)

loopermodule.loop:Disconnect()
1 Like

Yes, but it will busy wait before the loop_running is set to false.

Oh that heartbeat disconnect thing looks interesting, I will wait for other answers though, in case anyone can find something for a while loop.

To break a “while true” loop you need an outside source, so having an outside variable is basically the only way to break it

local running = true

while running do
    task.wait()
    --//code
end

running = false --//stops the loop

Why do you even have a loop in a module script? Just loop a module function from a regular script.

It won’t work since I still have to be inside the loop to break it.

Not just the same script.

You would be inside the loop if you loop the ModuleScript function from the script which requires the module.