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.
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
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
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
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.