Local script not deleting itself

Hey guys, just having a small (and probably very dumb!) issue, where I’m unable to make a script destroy itself

Here’s what i’m doing to try to destroy it / prevent it from working

            -- In the local script itself
			script:Destroy()
			script.Disabled = true
			Functions:Destroy(script)
            
			while task.wait(.1) do
				print("Heheheaw")
			end

            -- In the module called functions here
			function module:Destroy(Object)
				Object:Destroy()
			end

“Heheheaw” is still being printed every .1 seconds. Does anyone know what mistake i have made here? Please let me know if you do! Thank you for taking the time to read this :smile:

For the script

game:GetService("Debris"):AddItem(script, .1)
script.Disabled = true --gives it time to disable before its nil

or just put this in the loop

if script then

end

sadly, the first option seems to give the same result and the 2nd option is what i wanted to avoid. If i however print the scripts parent in a loop instead of the hehehehaw, the parent is nil as expected

This while loop, is preventing from the code below [calling the module] from running.
I suggest wrapping that loop with a coroutine or a spawn function.

That’s the only issue I can see here.

I was just using the code to test if the code was still running or not - if i removed that, would it infact actually be deleted?

If you are deleting it on that function(module), try wrap the while loop in a spawn function.

spawn(function()
--while loop here
end)

I don’t really mind what i delete it in, but i don’t see why i’d need to do it in a while loop since it’d just need to also still be running constantly

But then your code below the loop wont run
I assume the module helps u remove what you need

Just create a bool “Destroyed”. That’s all about it.

That’s wild. Doesn’t seem like it should work that way does it? XD

--script:Destroy()
--script.Disabled = true
Functions:Destroy(script)
task.wait() -- defer will cancel after first wait..here or once thru loop
            
while task.wait(.1) do
	print("Heheheaw")
end

-- In the module called functions here
function module:Destroy(Object)
-- task.delay will work too.. again after delay time, at next wait after called
	task.defer(function()
		Object:Destroy()
	end)
end

Pretty sure I had a version (that I didn’t save) with task.delay(n, function()... called from the LocalScript that worked if script.Disabled was commented out. That Disable seems to be able to keep parts of the script from working (e.g. keeps the fn in the task.delay from being able to Destroy the script), but it doesn’t kill the while true loop. :confused: script:Destroy() called from the LocalScript removed the script from the explorer and presumably set it’s parent to nil (didn’t check), but the loop is entered and keeps on going. Didn’t try this, but can maybe try putting wait between the Functions:Destroy line and the while loop and see if that’s enough for the script:Destroy() to do its thing.

1 Like

Could you expand on this? I don’t see what difference that would really make

I’ll give this a try later today, thanks!

local Destroyed:bool
Functions:Destroy(script)

while not Destroyed do
   task.wait(0.1)
   print("Heheheaw")
   Destroyed = Functions:IsDestroyed(script)
end