How to kill a function running in Another thread spawned via task.spawn

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to kill a function that is running inside of a task.spawn
  2. What is the issue? Include screenshots / videos if possible!
    i have the following function
task.spawn(function()
            while task.wait(options.listenTimer) do
                local output = httpService:JSONDecode(httpService:RequestAsync({
                    ['Url'] = options.URL .. '/WorldQL/Ping',
                    ['Method'] ='GET',
                    ['Headers'] = {['key'] =  WQLAPIKEY},
                }).Body)
                if output.failed then
                    error(output.message)
                end
                UnreadMessages = output.output.messages
            end
        end)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have not tried anything as there appears to be no dedicated function for killing task made with task.spawn or task in general

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
The Task is used for pinging the REST api so it doesen’t mark the client as dead
but after telling the client to disconnect using .disconnect it still tries to keep alive causing the entire script to crash

Edit:
also note:
I am okay with making a variable called smth like Connected which can be used to break out of the loop. if that is the reccommended course of action that is fine

Edit2:
link to ROJO project on github
WorldQL_RBXL/src/shared/WorldQL at main · walksanatora/WorldQL_RBXL (github.com)

local function task()
	while task.wait(options.listenTimer) do
		local output = httpService:JSONDecode(httpService:RequestAsync({
			['Url'] = options.URL .. '/WorldQL/Ping',
			['Method'] ='GET',
			['Headers'] = {['key'] =  WQLAPIKEY},
		}).Body)
		if output.failed then
			error(output.message)
		end
		UnreadMessages = output.output.messages
	end
end

local taskCoro = coroutine.create(task)

I personally would use coroutines to achieve a similar result, making use of yield to pause etc.

coroutine.yield(task)

but now dont i have to constantly run

coroutine.yield(task)

somewhere else
this just moves where i have to stop the function
because this is called in .connect
lemme edit my initial question linking to the ROJO project on github
I currently think that adding another fenv variable would probally be the best bet