Part.Touched dedect only NPC isntead player (SOLVED)

I have found this script in other posts as a solution. I have a npc in Workspace and there a part. I am trying to dedect if NPC touched that part. But the script just print “touched” when Player touched it instead NPC. Can someone help?

script:

local killnpc = script.Parent

killnpc.Touched:Connect(function(hit)
	print("touched btw")
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player == nil then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0
		end
	end
end)

Script location:

image

3 Likes
local KillPart, NPC = script.Parent, workspace.NPC -- Or Wherever You're NPC Is.

KillPart.Touched:Connect(function(Hit)
	if Hit:IsDescendantOf(NPC) then
		warn(Hit:GetFullName().. " Touched This.")
		Hit.Parent:WaitForChild("Humanoid").Health = 0
	end
end)

And if you want it to Disconnect after:

local KillPart, NPC = script.Parent, workspace.NPC -- Or Wherever You're NPC Is.

Connection = KillPart.Touched:Connect(function(Hit)
	if Hit:IsDescendantOf(NPC) then
		warn(Hit:GetFullName().. " Touched This.")

		Connection:Disconnect()

		Hit.Parent:WaitForChild("Humanoid").Health = 0
	end
end)
2 Likes

This one didn’t print anything when i touch to part or npc.

1 Like

Change “workspace.NPC” to “workspace.VS.NPC”

1 Like

I already did it, changed to NPC’s location with script.Parent.Parent.NPC

1 Like

Could you show whats inside the NPC, or if it atleast killed it.

1 Like

Ofc, there:

image
respawn script blank btw

1 Like

You could try and see if it’s even touching in the first place by doing:

local KillPart, NPC = script.Parent, script.Parent.Parent.NPC

Connection = KillPart.Touched:Connect(function(Hit)
	if Hit:IsDescendantOf(NPC) then
		warn(Hit:GetFullName().. " Touched This.")

		Connection:Disconnect()

		Hit.Parent:WaitForChild("Humanoid").Health = 0
	else -- Would See If Something Outside Of NPC's Descendants Were Touching
		warn("Hit Isn't A Decendant.. Source: ".. Hit:GetFullName())
	end
end)
1 Like

It printed when I touched, nothing printed when NPC touched…
image

1 Like

Under you’re NPC’s values (Such As The “Left Leg”), there should be booleans such as “CanTouch”, or “CanQuery” if “CanTouch” isn’t on, it won’t register.

image

1 Like

I can touch as a player

image

1 Like

Try replacing

local KillPart, NPC = script.Parent, script.Parent.Parent.NPC

with

local KillPart, NPC = script.Parent, workspace:FindFirstChild("iQeeYT")

and then put the latter in here:

Connection = KillPart.Touched:Connect(function(Hit)
NPC = script.Parent, workspace:FindFirstChild("iQeeYT")
	if Hit:IsDescendantOf(NPC) then
		warn(Hit:GetFullName().. " Touched This.")

		Connection:Disconnect()

		Hit.Parent:WaitForChild("Humanoid").Health = 0
	else -- Would See If Something Outside Of NPC's Descendants Were Touching
		warn("Hit Isn't A Decendant.. Source: ".. Hit:GetFullName())
	end
end)

I think he’s trying to detect only the NPC, not himself, though I don’t know what’s wrong with the NPC it doesn’t seem to not work for me.

1 Like

It just print:

--Workspace.iQeeYT.Red Flame Aura.Handle Touched This.  -  Server - stopScript:23

Not dedect NPC’s touch.

I think that he IS trying to detect himself. He’s talking about the ‘NPC’ as in himself. Look at his screenshots, he’s the one walking over those bricks. Now look at his comment, it worked. He’s the one.

His NPC isn’t being detected by the script for whatever reason, that’s his problem.

I am trying to dedect if NPC touched the part, not me.

Oh I see. My apologies then. Is the NPC moving towards the brick?

yea i change npc’s position with while loop, so it goes to brick straight. In the end NPC touches to brick. But it won’t dedect touched.

Does the NPC’s legs have the CanTouch property enabled?