How do I cancel a destroy function once I click on a part

As said in the title of this post, I would like to cancel a destroy function when I click on a part.

I have a post Here about how it’s going to go into the player’s backpack when clicked, but now I wonder how I can cancel a destroy function without destroying it in the backpack.

I’ve heard the word coroutine being used in posts but I don’t understand what it means, can someone elaborate that for me?

This post is just for me to learn things, as I begin scripting in the studio.

1 Like

do you mean that you no longer want that function to work? if so connections would come in handy, set up a connection for the event and once you don’t want the event to work disconnect its connection

(Copied from your previous post)
As an example

function onMouseClick(player: Player)

– //Backpack
local Backpack = player.Backpack

– //Parent
if Tool.Parent == PartToClick then
Tool.Parent = Backpack
else
warn(“Tool Parent:”, Tool.Parent)
end

end
— Set up a connection here
local Connection = ClickDetector.MouseClick:Connect(onMouseClick)

whenever you want the event to not work u can just disconnect it by doing

Connection:Disconnect()

and once the connection is disconnected it’ll no longer work

1 Like

In this case, my destroy function is passive, and I would like the function to stop after it is clicked. By using your logic, can I just use

Destory:Disconnect

like this?

1 Like

no you can not disconnect a function, you disconnect the event that triggers the function.
if you send the code i could try to help cause i didn’t really understand what u were trying to do

2 Likes

I’m overseas right now, so I can’t send the code. Aside from that, what’s coroutine? I would like to know more

local Part = Instance.new("Part")

Part.Touched:Once(function()
	--Only Triggers Once Forever, It Disconnects Itself
end)

replace :Connect to :Once

but if what ure asking is how to stop a script from going through, just put a IF statement condition
like

local Part = Instance.new("Part") -- The Subject
local Condition = false -- The Condition

task.spawn(function()
	-- A Seperate Thread
	task.wait(30) -- Timer Before Part Gets Destroyed
	
	if Condition == false then -- The IF Condition
		Part:Destroy() -- Destroy The Part
	end
end)

task.wait(5)

Condition = true -- The Condition Change, So The Destroy Wont Happen
1 Like

I’d recommend using the solution posted by Veexas, however there’s no reason to keep the task running or even make a condition check. Just use local task = task.spawn and then when the item is clicked with use task.cancel(task) to stop the task from running.

Here’s my version of their script:

local DestroyInstance = -- Make this whatever you're trying to destroy on click
local DestroyTime = 30
local ClickDetector = -- Make this the click detector instance

local TaskToCancel = task.spawn(function()
	task.wait(DestroyTime) -- Timer Before Part Gets Destroyed
	DestroyInstance:Destroy() -- Destroy The instance
end)

local Connection = ClickDetector.MouseClick:Connect(function() -- Detection for click
	if coroutine.status(Task) == "dead" then return end -- Check if the task is still running
	task.cancel(TaskToCancel) -- Cancel the destroy task
end)

.
Don’t disconnect after destroy:
You shouldn’t worry about disconnecting it if the clickdetector is parented to the instance you’re destroying as you mentioned in your post. I know some people implied that but it’s unneccessary.

If you want to disconnect the click detector if destroy time passes:
you want to disconnect after the wait time so the MouseClick is no longer being detected in which case just put that above the DestroyInstance:Destroy() line and remove the coroutine.status line.

Disclaimer:
Take this code with a pinch of salt, I’m writing it directly on here through my phone so some of the syntax may be incorrect.