Task.spawn not fully running

In this big long function inside a module script, I have a system where players are added to a debounce table and removed a little later to stop the function from being ran multiple times

it works fine most of the time but occassionally the task.spawn starts (it prints “starting task.spawn”) but never gets to finishing the 3 seconds

I’ve looked for a solution on the dev forum and stuff but couldn’t find one

heres the part of the script:

		task.spawn(function()
			print("starting task.spawn")
			task.wait(3)
			print("3 seconds over")

			local index = table.find(debouncePlayers, plr)
			if index then
				table.remove(debouncePlayers, index)
			else
				warn("player is not in table, not removing debounce")
			end
		end)
1 Like

Try task.delay to delay the thread for three seconds before it runs, rather than waiting inside the task.spawn.

If this does not work, could you check if the script that this function is being called in is local or global? Does this script ever get deleted? is it attached to the player?

I’ve just tried task.delay and it didn’t work. the script is a module script that only ever gets called on the server. The script that calls the module does get deleted AFTER the module function is called, but I don’t think that would stop a module script function from running, right?

task.spawn() will only stop running due to forceful termination (such as task.cancel()) or if the container that invoked the call stops running. Take this basic script for example:

task.spawn(function()
	print("Yielding")
	task.wait(3)
	print("Done")
end)

task.wait(1)
script.Disabled = true

The function immediately prints “Yielding” and begins to wait for 3 seconds. However, before the wait ends, the script is terminated by setting script.Disabled to true, so the function never prints “Done”. Your code is in a function defined by a module script, so any threads created in that function are tied to the script that invoked said function. If the calling script terminates, any threads tied to that script terminate with it.

1 Like

does deleting the script that calls the module (deleted after the module function is ran) cause the module script thread to end?

if the script that you called the module script from gets deleted, the thread created from task.spawn will also get deleted as it is attached to that script.

Try calling the module elsewhere or finding an alternative to deleting thecript

Yes, this would cause the thread to end. The script calling the function in that module runs all the threads created in that function, so if the script stops running, all the threads it was running end as well.

CC: @loltrol2121

This is not entirely correct. The thread a ModuleScript runs in is not tied to the lifetime of the script that required it. That would actually be disastrous as other scripts might have required the module.

What I believe is happening in OP’s case is that they are calling a function from a Script that was defined by a ModuleScript. Since the function gets called in the Script’s thread, any subsequent threads spawned by the function get tied to the Script. If the thread was spawned by the ModuleScript itself, it wouldn’t die.

3 Likes

Ah, thanks for clarifying! I’ll edit my message so anyone referencing the solution for this topic isn’t confused.

1 Like

That not true.
Thread can become “dead” if nothing keeps yielding it and it had finished all insturctions.
task.cancel would just force it into such state.

image
Yeah can confirm.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.