Why does this explosion part ignore the wait?

Hello, I’m trying to make this TNT thing and make it wait a certain time before it explodes, but for some reason it ignore the wait(3) and just explodes immediately.

local exist = false


script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and exist == false then
	exist = true
	local doorTNT = game.ServerStorage.doorTNT:Clone()
	doorTNT.Parent = game.Workspace["door explode"]
	local explosion = Instance.new("Explosion")
	explosion.Parent = game.Workspace["door explode"]:WaitForChild("doorTNT")
	explosion.BlastPressure = 0
	explosion.Position = doorTNT.Position
	wait(3)
    explosion:Destroy()
	game.Workspace["door explode"].CanCollide = false
	else
		print("TNT already placed!")
 end
end)

Explosions occur the instant they’re parented to the workspace. Instead of waiting 3 seconds to destroy the explosion, wait 3 seconds before parenting the explosion. You also don’t need to call destroy on it, as the explosion will remove itself automatically once it is finished.

    wait(3)
	local explosion = Instance.new("Explosion")
	explosion.BlastPressure = 0
	explosion.Position = doorTNT.Position
    explosion.Parent = game.Workspace["door explode"]:WaitForChild("doorTNT")
2 Likes

Thanks, I never knew that. Learn something new everyday.

Ah, one more thing. Always ensure you set the properties of an instance first before you parent it. If you parent it first, it becomes more expensive and less performant to change properties later. The explosion may also not adhere to what you’ve set.

I made a quick edit to the code I posted to parent last instead of first.

Even though you don’t use the parent argument of Instance.new, this post is somewhat related:

1 Like