Instances not destroying after 15 seconds

What do you want to achieve? For the instance to wait 15 seconds before being destroyed

  1. What is the issue? instance is not destroying after 15 seconds

  2. What solutions have you tried so far? I tested setting the wait to 1 seconds and behold it worked, all instances that lost health at once were destroyed a second later however I need it to be 15 seconds. I have tried coroutines but that doesn’t change things.

local HealthHandler = {}

function HealthHandler.healthDeduction(health, instance)
	
	instance.Health.Value = health
	
	coroutine.wrap(function()

	if instance.Health.Value <= 0 then
			instance.Anchored = false
			instance.Material = Enum.Material.CorrodedMetal
			
			print("got to this point")
			
			wait(15)
			
			instance:Destroy()
			print("Destroyed " .. instance.Name)
		elseif instance.Health.Value ~= 150 then
			instance.Material = Enum.Material.CorrodedMetal
			print("not destroyed")
		end
	end)()
end

return HealthHandler

It is due to the coroutine.wrap

U need to change it to a function then it will work

so do I use coroutine.wrap from where the function is being called upon?

And also do u have a script to activate the function

when I do this it will only destroy 1 instance at a time rather than before when it affects all instances (makes them be corroded) but doesnt destroy them

yes, i am calling it from a seperate module script which has a function that needs this “healthDeduction” function, the module script is being used by a server script so I don’t see why this isn’t working

I think it is stop by a condition,but are there any errors when running the code

no, it just seems like none of the instances are waiting 15 seconds to be destroyed, which causes a lot of performance issues since a bunch of parts are being unanchored and are staying in the workspace

U can use
game:GetService('Debris'):AddItem(instance,15)
or

task.delay(15,function()
     instance:Destroy()
end)

Thank you this seemed to do it :+1:

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