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

Doesn't work in a NPC but works inside a part, why?

So, I'm creating a zombie NPC that damages a player on touch and the script worked perfectly fine until I decided to add a debounce to it. The script still works, however, the script works inside any part that isn't inside a NPC. Once it's placed inside a part inside a NPC it won't work. I don't understand, someone please help!

debounce = false
script.Parent.Touched:connect(function(hit)
	h = hit.Parent:FindFirstChild("Humanoid")
	if debounce == false then 
		debounce = true 
		h.Health = h.Health - 50
		wait(2)
		debounce = false
	end
end)

3 comments

0

Make sure to check that the 'hit' has a Humanoid, because if it touches an accessory, the script world break, since it wouldn't be able to find a humanoid in the accessory.

0

The torso or whatever this script is inside of is most likely hitting the other parts of the NPC, causing the debounce to go off.

0

Do you know how I could fix that?

1 answer

✓ Accepted answer
debounce = false

for i, v in pairs(script.Parent:GetChildren()) do
	if v.ClassName == "Part" then
		v.Touched:connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local h = hit.Parent:FindFirstChild("Humanoid")
				if debounce == false then 
					debounce = true 
					h.Health = h.Health - 50
					wait(2)
					debounce = false
				end
			end
	
		end)
	end
end

Put that script inside of the NPC

3 comments

0

It's still not working! D:

0

Are you putting the script inside of the NPC's model? (Not the humanoid)

0

Thank you for clarifying. I was placing the script inside of a part located in the NPC.

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