Task not getting cancelled

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’m trying to cancel a task so the effect doesnt get it’s value reset

  2. What is the issue? Include screenshots / videos if possible!
    The task isn’t getting cancelled so the value keeps getting reset

local module = {}

local effects_in_place = {}

function module:Add_New_Effect(who, what: string, duration: number, amount: number, time_to_wait_between_loops: number, stackable: boolean)


	spawn(function()

		--if game:GetService("Players"):GetPlayerFromCharacter(who) then
		for i,v in ipairs(effects_in_place) do

			if v.name == what then
				if v.stackable == false and v.owner == who then -- check if the effect is stackable
					if v.lightingeffect == true and game:GetService("Players"):GetPlayerFromCharacter(who) then
						game.ReplicatedStorage.lighting:FireClient(game:GetService("Players"):GetPlayerFromCharacter(who), "ColorCorrection", 0.2, Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, true, 0, 0.1, true, 0.1, Color3.new(1, 1, 1))
					end
					if v.name == "speed" then
						who.Humanoid.WalkSpeed -= v.amountt
					end
					if v.name == "protection" then
						who.enemydmgmultiplier.Value += 0.15
					end
					if v.name == "strength" then
						who.dmgmultiplier.Value -= 0.15
					end
					if v.name == "berserk" then
						who.specialeffects.berserk.Value = false
					end
					task.cancel(v[what]) -- not getting cancelled
					table.remove(effects_in_place, table.find(effects_in_place, v))
				end
			end
		end
		
		if what == "expose" then -- issue
			local newtable = {
				name = what, 
				stackable = stackable, 
				owner = who,
				amountt = amount,
				lightingeffect = false,
				["expose"] = task.spawn(function()


					who.enemydmgmultiplier.Value += 0.05

					task.wait(duration)
					print("task didnt get cancelled") -- this is still printing 
					who.enemydmgmultiplier.Value = 1



				end)
			}
			table.insert(effects_in_place, newtable) 
			task.wait(duration)
			task.cancel(newtable.expose) 
			table.remove(effects_in_place, table.find(effects_in_place, newtable)) 
		end
     

This is a piece of code from the module

Maybe try adding checks if the task exists before cancelling it.

But the issue is that the task is still printing after “cancelling” it

Task.cancel can’t cancel a thread that has already resumed, so presumably although the spawned task yields, it is still resuming before the thread asking it to cancel.
In the spawned task, you could try increasing the yield duration in the task.wait() ( or just add in an extra task.wait() ), that might be enough to guarantee the order of execution for your threads.

By resuming you mean it finished the code inside of the task?

Do not cancel the thread inside of the thread. Set a completed variable outside of the thread which you will set to true after completing the thread. Use another thread to monitor when that value turns true by doing something like this:

task.spawn(function()
	repeat task.wait() until ThreadCompleted
	task.cancel(ThreadVar) -- Thread Var is the Variable for your thread.
end)

That should hopefully work.

1 Like

Sorry if im asking too much but would it cause a memory leak when the thread gets cancelled?

I don’t believe so. Luau is pretty memory safe in general.

It worked, thank you

charsssss

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