Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Making an NPC kill on touch?

I want to make an NPC kill anyone who touches him. My approach was to first create a function onTouch that will set health of who touches him to 0 (health = 0) and then to set each part of the same itself to activate this function on touch using the following code:

for i,v in pairs(script.Parent:GetDescendants()) do
	if v:IsA("Part") or v:IsA("MeshPart") then 
		v.Touched:connect(onTouch)
	end 
end

Well, something seems to be missing! It kills me except for the following case: when I jump on the NPC's hair, it's not killing me.

9 comments

0

Hair is an accesory, it isn't a basepart, add it to the 'or' list (or v:IsA('Accessory'))--> Hope I spelled that right xD

0

Yes I agree

0

but then it says that "Touched is not a valid member of Accessory"

0

Oh yeah, the accesory isn't collidable, there is a part inside it called handle (that's a basepart)

0

Oh...???? Maybe you could turn cancolide off for the accessories? Then you would fall straight through to the NPC's head and be killed? I really don't know ether.

0

I changed my code to if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then but the hair still doesn't kill me.

0

Use, elseif v:IsA('Accessory') then v:FindFirstChild('Handle').Touched:Connect(onTouch)

0

Not working. I mean to say that when I jump on the NPC's hair it still doesn't kill me.

0

Hm, you probably have the accesory still in your first 'if' statement then (if not, can you update the code?)

1 answer

You'll want to detect if an Instance within is an Accoutrement (the class that Accessorys and Hats inherit from; do NOT detect for only Accessorys because old scripts will use the Hat class) and then connect to the Handle's touched event. You also probably can use GetChildren() instead of GetDescendants().

for i,v in pairs(script.Parent:GetDescendants()) do
    if v:IsA("Part") or v:IsA("MeshPart") then --We'll want to keep MeshPart hanging if the NPC is a R16 rig.
        v.Touched:Connect(onTouch)
	elseif v:IsA("Accoutrement") then -- if it's a Hat or Accessory then
		v.Handle.Touched:Connect(onTouch) -- connect to the Handle's touched event
    end 
end

np



now, plz accept if this helped or i will be salty

#edit

it don't work???? nani

well

err

you can always try turning the hair's CanCollide off

1 comment

0

it doesn't work - i can still jump on the npc hair and live

This discussion is preserved as it was. New answers, comments, and votes are closed.