How do I replicae javascript clearInterval in roblox lua?

I tried doing this:

function setInterval(func, ms)
  spawn(function()
    while wait(ms / 1000) do
      func()
    end
  end)
end

setInterval(function()
  -- funcs
end, 5000)

However I can’t figure out how to make the clearInterval func

Im going to assume the 5000 is milliseconds, so.

while true do
    --Code
    wait(5)
end

what about clearInterval?
30 charrrrrrrrrrrrrrr

So essentially you want to stop the loop?

yes this ies what I want todo.

why are you doing this…?

anyways, here is something i wrote up.

local thread = {}

thread.__index = thread

function thread.new(callback, timeout)
	local self = setmetatable({}, thread)

	self.running = false
	self.callback = callback
	self.timeout = timeout

	return self
end

function thread:run()
	self.running = true
	
	local runner = coroutine.create(function()
		while self.running do
			self.callback()

			wait(self.timeout / 1000)
		end
	end)

	coroutine.resume(runner)

	return runner
end

function thread:stop()
	self.running = false
	coroutine.yield()
end

local myThread = thread.new(function() print("when the imposter is sus!") end, 1000)
myThread:run()
wait(5)
myThread:stop()
1 Like

Just add break inside the loop and it will stop.

:vulcan_salute: