Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I make my npc only attack players?

Asked by 4 years ago

So I have an npc named " Enemy Native " and it follows you within a distance of 20 studs and does .15 damage to you. But it attacks every other humanoid in its distance. How would I make it not do this and make it attack only the player, as I will be adding in different humanoids aswell. Here is the follow script.

function findNearestPlayer(Position)
    wait(0.3)
    local List = game.Workspace:children()
    local Torso = nil
    local Distance = 20
    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 - Position).magnitude < Distance then
                    Torso = Temp
                    Distance = (Temp.Position - Position).magnitude
                end
            end
        end
    end
    return Torso
end




while true do
    local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

1 answer

Log in to vote
0
Answered by 4 years ago
local humanoid = script.Parent.Humanoid
local Target = game.Players:FindFirstChild("Part")
if Target.Name = HumanoidRootPart then
    Humanoid:MoveTo(Target.Position)
end

I'm not an expert at scripting. This might not work, but try it.

0
Im not an expert or even pro so where would I put this in the script? MrMeeper9 39 — 4y
0
im not sure. ScriptsALot 91 — 4y
Ad

Answer this question