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