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(TaskToCancel) == "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.

what if my parts are in a server storage, what do i do then

Use a server script, clickdetectors still fire on the server and just set the destroyinstance to the part.

or just store the task.spawn in a variable, and use task.cancel, should work if I remember right. But your solution works too.

it works, to an extent? it just became a more complicated debris. can you show a video example of what its supposed to look like?

the click and other stuff works, and the part goes into the player’s backpack, but it gets destroyed either way like debris

Did you copy and paste my code, or did you go through it.

This line specificly:
if coroutine.status(Task) == “dead” then return end
Should say
if coroutine.status(TaskToCancel) == “dead” then return end
That would be why it destroys either way

Sorry about that correction, essentially whats happening is it’s checking if an empty task is dead, which it always will be so the task never actually cancels. Your code should work after this correction is made. I’ll update the original post too so you can just copy and paste the whole thing if you want.

2 Likes

This is so vague that I cannot fathom what you want to do or why you want to do it.

You want to cancel a destroy function on a part that has been clicked-on and added to a players backpack. Now from within the backpack you wish to not destroy the part in the backpack? Then don’t destroy it! Am I missing something?

I want it so that it destroys when its NOT in the backpack, and when it is, the destroy function is cancelled.

How are you creating the parts?

From a server script requiring a module script in the script service, parts are in server storage

I think what you want to achieve is easy, it just depends on how the parts are made, when they are made, why they are made, the difference between the parts added to the backpack and those not added to the backpack, why they need to be destroyed as they are, and why they can’t be created, stored, and destroyed using a different system.

What? I don’t understand what you’re saying at all. They’re spawned into the workspace by cloning them from the server storage and placed under a folder, and destroys itself after a while. However, when you click on it I want the destroy function to be cancelled.