script:Destroy() and coroutine issue

I’m at a bit of a block here. I want to insert a string into a table, but the code won’t run if the script is destroyed. Is there a way to make this happen even if the script is destroyed?

Example script

local ObjectNames = {} -- Table that contains object names.

function InsertName(Name: string)
	task.wait(2) -- waits 2 seconds obviously.
	table.insert(ObjectNames, Name) -- inserts the object name into the "ObjectNames" table
end

local Task = coroutine.create(InsertName) -- creates a coroutine (coroutines are used to make code run without delaying the script)
coroutine.resume(Task) -- makes the coroutine run.

script:Destroy() -- destroys the script, but the coroutine won't run, because the script was destroyed.

Have a different script Destroy() that script. May not work however as you made this a coroutine.
Maybe set up a break point in that. if x=true then break end

This didn’t help, unfortunately. Thanks anyway!

I totally read that wrong … ya you can’t have a coroutine running off a Destroyed script.
If the script is done you should be fine leaving it.

Well, alright I suppose. Thanks for the help.

1 Like

I was kind of hoping for something like Debris:AddItem() which runs even if the script is destroyed.

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