How do I stop a triggered function from running

local connections = {}

CollectionService:GetInstanceAddedSignal("TestTag"):Connect(function(instance)
	connections[instance] = task.spawn(function()
		local testProximity = instance.ProximityPrompt;

		testProximity.Triggered:Connect(function(player)
			task.wait(1)
			print("proximity first test")
			task.wait(3)
			print("proximity second test")
		end)
		
		task.wait(5)
		print("task first test")
		task.wait(2)
		print("task second test")
	end)
end)

CollectionService:GetInstanceRemovedSignal("TestTag"):Connect(function(instance)
	task.cancel(connections[instance])
end)

I thought that closing this task would also stop the triggered function. Now I wanted to disconnect the proximity triggered function but it didnt work. I assume the disconnect only disconnects it from working but doesnt stop the function itself. Now how would I do this?

All I want to do is cancel everything inside the task after the tag has been removed.

Add a Variable to the Connection

local connection = testProximity.Triggered:Connect(function(player)
end)
	
connection:Disconnect() -- Disconnects the Event

Thats what I did, it just doesnt stop it sadly.

What I am trying to achieve is that the function stops even while running. Not only disconnect the event basically.

local Active = true

-- inside event
if not Active then -- if Active is false
   return -- ends function
end
1 Like

Thanks again, this wouldnt stop the function tho, because it would only run once the event is triggered.

What I basically want is the triggered function to stop completely. So the task.wait and stuff dont run even after the function has been triggered. I assume theres no way to do it.

return stops the function, normally it returns data. But in this case it returns nil

Yes but the if only checks once in the event. So the event would fully run and then check if active is still active.

I feel like using couretines may be of benefit here. That’s not my speciality though, so someone else can expand

1 Like

Tried using coroutines but they also dont stop the triggered while its running.

Have a look at Couretine.yield. May be of use

Would this work?

local t
function Task(p)
t = task.spawn(function()

print(p)
task.wait(5)
print("oof")
end)

end


local A = ProximityPrompt.Triggered:Connect(Task)



A:Disconnect()
task.cancel(t)

coroutine.yield only pauses the coroutine until called again, it doesnt kill it.

Nope, since disconnect just stops the event from working but doesnt stop it if its currently running.

That isn’t all they have in their code. Actually test the code provided instead of just dismissing it from a quick glance over it

ok, looking into the documentation of coroutines, coroutine.close is what you would use to kill it

Yep, coroutines worked the same for me on this issue.

So you tried this?

local co = coroutine.create(function()
-- code
end)
-- within connection:
coroutine.resume(co)

-- Disconnecting:
connection:Disconnect()
coroutine.close(co) -- coroutine has to be running for this to work

yep exactly that, and the connection stops so the other prints dont work, but the prints inside triggered still work.

Did you try this as well?

local connections = {}
local c
CollectionService:GetInstanceAddedSignal("TestTag"):Connect(function(instance)
	connections[instance] = task.spawn(function()
		local testProximity = instance.ProximityPrompt;

		c = testProximity.Triggered:Connect(function(player)
			task.wait(1)
			print("proximity first test")
			task.wait(3)
			print("proximity second test")
		end)
		
		task.wait(5)
		print("task first test")
		task.wait(2)
		print("task second test")
	end)
end)

CollectionService:GetInstanceRemovedSignal("TestTag"):Connect(function(instance)
	task.cancel(connections[instance])
    c:Disconnect()
    connections[instance] = nil
    
end)

@LaxoLPYT It works, im sure why yours doesnt

Random Script i tested

local connection = script.Parent.Triggered


thread = nil
c = connection:Connect(function()
	thread = task.spawn(function()
	while wait() do
		print("R")
	end	
	end)
end)
wait(5)
task.cancel(thread)
c:Disconnect()