I’ve done some testing and I’ve gotten it to work.
It detects both the player and the NPC.
Here’s a video with the server’s point of view:
Here is the code (it’s a script placed inside of a part):
local RunService = game:GetService("RunService")
local NPC = workspace:WaitForChild("NPC")
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
print("Humanoid found! It's health is", humanoid.Health)
end)
-- Ignore run service, it's just used here to move the NPC to the part
RunService.Heartbeat:Connect(function()
NPC.Humanoid:MoveTo(script.Parent.CFrame.Position)
end)
The script is a little different but using humanoid.Health = 0
would work in this case as it’s detecting the health.
If you want to detect only the NPC and not any players, try this:
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local NPC = workspace:WaitForChild("NPC")
script.Parent.Touched:Connect(function(hit)
local isPlayer = Players:GetPlayerFromCharacter(hit.Parent)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid or isPlayer then return end
print("Humanoid found! Health is", humanoid.Health)
end)
-- Ignore run service, it's just used here to move the NPC to the part
RunService.Heartbeat:Connect(function()
NPC.Humanoid:MoveTo(script.Parent.CFrame.Position)
end)
So i ignored others and used this only:
But it won’t print again.
local NPC = workspace.VS:WaitForChild("NPC")
script.Parent.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if not humanoid then return end
print("Humanoid found! It's health is", humanoid.Health)
end)
Weird. Try this:
--local NPC = workspace.VS:WaitForChild("NPC")
script.Parent.Touched:Connect(function(hit)
print(hit:GetFullName())
end)
If nothing shows up, it isn’t detecting anything.
This just prints anything the part touches.
Also, check if the part has the CanTouch
property enabled.
i tried it too, it did not work, here video:
Did you check the part’s properties?
Ofc, all work when i touched, but not for NPC
I wonder if it’s just that specific NPC.
Try importing a basic Rig from the Rig Builder in the Avatar
section.
Move it over the part and see if anything prints?
oh yeah… It works when i make with it. But what’s wrong with that NPC? All fine…
I’m honestly not sure. Could you make a separate place, import the NPC there and send me the RBXL so I can see what’s wrong with it?
Or you can use the rig you imported, copy the meshes & accessories over to it and see if it works that way.
Even my first code works now with rig builder that i posted when needing help
Ah I actually forgot that you can import files instead of places. Thanks for reminding me. I’ll take a look now.
it should work now i guess
local Players = game:GetService("Players")
local killnpc = script.Parent
local connection
connection = killnpc.Touched:Connect(function(hit)
if hit.Parent:IsA("Model") then
local getHitModel = hit.Parent
local checkIfIsNotAPlayerModel = Players:GetPlayerFromCharacter(getHitModel)
if checkIfIsNotAPlayerModel == nil then
local CheckForHumanoid = getHitModel:FindFirstChild("Humanoid")
if CheckForHumanoid then
CheckForHumanoid.Health = 0
end
else
if checkIfIsNotAPlayerModel:IsDescendantOf(Players) then
return
else
local SecondCheckForHumanoid = getHitModel:FindFirstChild("Humanoid")
if SecondCheckForHumanoid then
SecondCheckForHumanoid.Health = 0
end
end
end
else
return
end
end)
Thank you, the guy found it’s not about scripts. All about the NPC. I chagned NPC model now the script worked. But we are trying to figure out what’s wrong with NPC
The HumanoidRootPart
was anchored. I’ve unanchored it and…
If this fixed it for you, marking this as the solution would be appreciated
Thank you all others and you! They helped alot.
Oh may i ask something else?
It dedect when NPC touched on Server side, but i make the NPC move on Local side so it won’t be same for everyone. But now it won’t dedect when it touched in game for LocalPlayer view.
That’s because you’re moving the NPC from the client and not the server. You’re using .Touched
in a Script
and not a LocalScript
, so it won’t detect these changes.
Just move the code to a LocalScript
and it should do the trick. Make sure to update the variables though as the LocalScript
can’t be in a part that is in the Workspace
.