Instance:Destroy() not working

So I’m making a knife script, but I have a problem. For some strange, arbitrary reason the thrown knives JUST WONT DESTROY THEMSELVES. I have no idea how or why, but it just won’t work.

Here is the relevant code. I’m including as much as possible because I have no idea what could be causing this.

throwRE.OnServerEvent:Connect(function(plr, mouseHit, scale)
	local character = plr.Character
	
	if not character or not character:FindFirstChild('Humanoid') then return end
	if iscooldown then return end
	iscooldown = true
	knife.Cooldowned.Value = true
	character.Humanoid:LoadAnimation(throwAnim):Play()
	local knifeClone = handle:Clone()
	knifeClone.Velocity = mouseHit.LookVector * (speed * scale)
	knifeClone.Parent = workspace
	knifeClone:WaitForChild('WeldConstraint'):Destroy()
	handle.Transparency = 1
	
	knifeClone.CFrame = CFrame.new(knifeClone.Position, mouseHit.LookVector * 300)
	
	local bav = Instance.new("BodyAngularVelocity")
	bav.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
	bav.AngularVelocity = knifeClone.CFrame:VectorToWorldSpace(Vector3.new(-400 * scale, 0, 0))


	bav.Parent = knifeClone
	
	game.ReplicatedStorage.ClientKnife:FireAllClients(knifeClone, knife.Parent)
	
	knifeClone.Touched:Connect(function(touched)
		if touched.Transparency < 1 and not knife.Parent:IsAncestorOf(touched) and not knifeClone.Anchored then
			local humanoid = touched.Parent:FindFirstChild('Humanoid') or touched.Parent.Parent:FindFirstChild('Humanoid')
			
			if humanoid then
				humanoid:TakeDamage(100)
				animator = humanoid:WaitForChild("Animator")
				if humanoid.Health <= 0 then
					local killtrack = animator:LoadAnimation(killanim)
					killtrack:Play(.001)
				end

			end
			
			knifeClone.Anchored = true
			knifeClone.CFrame = CFrame.new(knifeClone.Position, touched.Position)
			knifeClone:FindFirstChild('MeshPart').Transparency = 0
			knifeClone.Transparency = 1
			wait(cooldown)
			knifeClone:Destroy()
			
			
		end
	end)
	wait(cooldown)
	iscooldown = false
	knife.Cooldowned.Value = false
	handle.Transparency = 0
end)

The knives are becoming Anchored and the transparency thing is working (it’s created so the knives are more likely to stick to the surface instead of floating a bit in front of it) but the knife isn’t destroying itself.

I’ve also tried creating a loop inside of the cloned knife that will destroy itself after being anchored, AND a script that will automatically clean out all anchored parts named “MeshPart” (the name of the cloned knife) and are parented directly to workspace. Neither work at all.

What on earth is happening?

It says wait(cooldown) before destroy, but it doesn’t define cooldown anywhere in the code, how long is cooldown and are you sure you are waiting that length?

Cooldown is only 5 seconds, and is defined before. Cooldown isn’t used in the other methods I described trying and the thing still fails to delete itself without the wait there anyways.

Try putting a print statement right before the Destroy() to make sure your coding is reaching it.

After adding a print statement right before the wait, right after the wait, and right after the destroy, all three are triggered and yet the knife isn’t destroyed.

I believe :Remove() instead of :Destroy() could help you out.

:Remove() is deprecated, use :Destroy()

3 Likes

Maybe setting the parent to nil would work. You’ll then want to set any references to nil.

:Remove() doesn’t even work anyways. :man_shrugging:

This also doesn’t work. Why? Who knows.

My bad, I’m new at coding.
Maybe use
delay(cooldown, function()

end)

Oh, that’s weird. I’ve never truly encountered such an issue using :Destroy().

no don’t use delay(), using delay() and spawn() is considered bad practice.

@sdenison are you 100% sure when you do Instance:Destroy() that that instances is not getting destroyed? maybe it is getting destroyed but there is some other flaw in the code like it’s creating a bunch of instances and only destroying 1 so it looks like it’s not being destroyed.

Is the knife supposed to be destroyed when it doesn’t detect anything for a few seconds?

The knife is destroyed when it hits a surface and becomes anchored.

I’m checking the explorer, and only one instance is being created.
Signing off for the moment, be back soon.

Unrelated, but Humanoid:LoadAnimation() is deprecated.

Maybe use Debris Service to delete the part.
Sorry if I’m wrong.

Yeah, something like ‘game:GetService(‘Debris’):AddItem(knifeClone, 0)’

1 Like

If you don’t know how Debris Service works, here is the link for you: Debris | Roblox Creator Documentation