How do I make a whole enemy get destroyed when a specific part of them gets jumped on?

I’m trying to make an enemy similar to a goomba and I already have the script to kill the player in every body part except the head because I want them to get destroyed when they get jumped on but I can’t figure out how to script the head so when the player touches it the goomba gets destroyed.

I’ve already tried using touched, hit, and destroy() but I’m either coding it wrong or that’s not what I’m supposed to do. How should I do this?

1 Like

Hi, i think that i can help you but i need your touched, hit and destroy code to see what do you need.

Sorry, I already deleted the script and don’t know how to make it again.

It basically said if the goomba is hit by a humanoid, destroy the goomba.

Dont worry, try this:

local Enemy = script.Parent
local Hitbox = Enemy:WaitForChild("NameOfTheHitbox")

Hitbox.Touched:Connect(function(hit)
	if hit:IsA("BasePart") then
		local players = game.Players:GetChildren()
		for _,Player in pairs(players) do
			if Player.Name == Enemy.Name then
				Enemy:Destroy()
			end
		end
	end
end)

You will need to put a script in the enemy model and a hitbox to detect when hit something.

1 Like

Sorry, I’m new to scripting. How do I add a hitbox?

Here is a video i explain all, make sure to weld the hitbox with the enemy!

3 Likes

Ok, thank you so much!

1 Like

if it work’s make sure to make your post as solved!

PD: No problem, i like to help people

1 Like

Ok, only one problem. For some reason when I replace print("Touched!") with script.Parent.Parent:Destroy() it destroys the enemy before I hit it. How do I fix it?

can you give me your script?
so i can see a solution

This is what I have:

script.Parent.Touched:Connect(function(hit)
	if hit:IsA("BasePart") then
		script.Parent.Parent:Destroy()
	end
end)

The only thing I changed from your’s was changing the print to destroy.

try this:

script.Parent.Touched:Connect(function(hit)
	if hit:IsA("BasePart") and not hit:IsDescendantOf(script.Parent.Parent) and not hit.Name == "Baseplate" then
		script.Parent.Parent:Destroy()
	end
end)

Ok, now it doesn’t destroy on the ground but it won’t destroy when the player hits it either.

can you send me the file?
i will se all of that

Sure, I’ll send it when studio starts working again. It broke for me.

Ok, here it is: Super Nario Bros.rbxl (90.8 KB)

Ok, just delete the and not hit.Name == "Grass"
the script has to look like this:

script.Parent.Touched:Connect(function(hit)
	if hit:IsA("BasePart") and not hit:IsDescendantOf(script.Parent.Parent) then
		script.Parent.Parent:Destroy()
	end
end) 
1 Like

It works! Thank you so much, now I can make the rest of the game!

1 Like