How do I make a local function stop outside of the function

I have a local function, I just want to know how to make it stop outside of the function (In the same script).

Could you elaborate on the code and what your function is doing?

From what I understand you have a function that runs forever and you want to be able to stop it when something happens. Is that correct?

Nah, it’s that my function has a lot of wait() in it, and I have external lines of code where I want to stop it instantly.

I’d recommend using a loop instead of a lot of wait() and I think it’s best to use RunService for the loop as it updates each frame rendered and is very reliable then you could just stop the loop with break just the same as any other loop.

If you could post the code we could better assist you as I’m not sure exactly what you’re asking for.

Could you not use coroutines and run that while maintaining it within the script?

Yep you should definitely post your code so it’s more clearer. Otherwise I’ll try to point to this other post by @MaliciousAlliance which also has waiting:

If you have a sequence of events with a lot of waiting like a game round script I suggest promises as it will look neater, and have the function the cancel: with a callback oncancel code.

Here is the code. Basically I want the clock to reset every time the game starts. The clock goes up by 1 hour ever 20 seconds.

local RS = game:GetService("ReplicatedStorage")
local clockTime = RS.ClockTime

local function time()
	clockTime.Value = "12 AM"
	wait(20)
	for i = 1, 6, 1 do
		clockTime.Value = i.." AM"
		wait(20)
	end
	RS.GameInProgress.Value = false
end

RS.GameStarted.Event:Connect(function()
	--I want to stop the function here
	task.wait()
	time()
end)

The game has 15 or more seconds of intermission until it starts again. Also setting the GameInProgress value to false ends the game.

To make a script stop you can either do
script.disabled = true

but to make a certain part of a script stop??
you’d have to use multiple scripts and make this sort of “script web”

Pretty sure that you can use remotefunctions and remoteevents to transfer variables

I’m not trying to transfer any values, and I’m not trying to disable the whole script.

You could probably do this by using a simple flag (or variable that holds state) to determine when to break out of the loop.

Another option is possibly having like a timer module that handles logic and events specifically for the timer.

So you could have a timer class that looks like the following:

local timer = {}

local RS = game:GetService("ReplicatedStorage")
local clockTime = RS.ClockTime

local SECONDS_PER_HOUR = 20 --//How many seconds in an hour
local MAX_HOUR = 6 --//When to stop the timer from incrementing

local function onTimerCompletion() --//Handles logic for when the timer fully completes
	RS.GameInProgress.Value = false
end

function timer:Start() --//Starts the timer
	--//See if a current timer event already exists
	if self.updateListener then self:Stop() end
	
	local timeElapsed = 0
	local currentHour = 12
	
	clockTime.Value = "12 AM"
	
	self.updateListener = game:GetService("RunService").Heartbeat:Connect(function(dt)
		timeElapsed += dt
		
		if timeElapsed >= SECONDS_PER_HOUR then
			currentHour = (currentHour == 12 and 1) or currentHour + 1
			clockTime.Value = currentHour.." AM"

			if currentHour == MAX_HOUR then
				--//Stop timer if the max hour has been reached
				self:Stop()
				onTimerCompletion()
			end
			
			timeElapsed = 0
		end
	end)
end

function timer:Stop() --//Stops the timer
	self.updateListener:Disconnect()
end

return timer

And then handle it from a controller script:

local RS = game:GetService("ReplicatedStorage")

local timerController = require(pathToTheModule)

local function startTimer()
timerController:Start()
end

RS.GameStarted.Event:Connect(function()
startTimer()
end)

Hope this helps. :slight_smile:

1 Like

I would use Sharpies solution, I don’t think there is any way to do this within the same script.

Guys I figured it out by instead of waiting 20 seconds, I just do a for loop and check if the game is over every 1 second. Thanks for the help though!

I set Sharpie’s comment to the solution because it seems like the way to do it though.