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?
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.
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?
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, 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)