Hello Developers! I need some help with my NPC monsters. So I have made an NPC that is supposed to follow a player then hit them and kill them.
The way I made it follow players is that I used this script Inside of the Monster:
local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 1000000000000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
while true do
wait(math.random(1,5))
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
Then I put a kill on touch script in all the body parts like the torso and the lower arm etc.
This is the kill on touch script:
function onTouched(hit)
if not hit or not hit.Parent then return end
local human = hit.Parent:findFirstChild("Humanoid")
if human and human:IsA("Humanoid") then
human:TakeDamage(100)
end
end
script.Parent.Touched:connect(onTouched)
But the NPC just follows other NPCs and then kills them. How can I modify these scripts to only follow players and only do damage to players?
Oh unfortunately I want it to be R15 also searching for a backpack seems a little complicated. If there is another way I will use it if not i will use yours
function onTouched(hit)
if not hit or not hit.Parent or not game.Players:GetPlayerFromCharacter(hit.Parent) then return end
local human = hit.Parent:findFirstChild("Humanoid")
if human and human:IsA("Humanoid") then
human:TakeDamage(100)
end
end
script.Parent.Touched:connect(onTouched)