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

Why does my npc attack my other npc?

Asked by 4 years ago
Edited 4 years ago

So I have a npc that attacks anything humanoid this is the 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
if game.Players:GetPlayerFromCharacter(Temp) then
                    Distance = (Temp.Position - Position).magnitude
                    end
                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

Why does he attack anything thats humanoid and not only players? I put line 17 in but it still doesn't work. I want him just to attack players. The duplicated npc has the exact same everything from the first one. I just clicked ctrl d and moved my npc2 next to my npc 1 to test. The script I pasted in my post is the follow script which is the movement. There is also a damage script and animation script for walking and stuff but that doesnt matter.

3 answers

Log in to vote
0
Answered by 4 years ago
function findNearestPlayer(Position)
    wait(0.3)
    local List = game.Workspace:GetChildren() --it's :GetChildren() not :Children()
    local Target --Just need this
    local Distance = 20

    for i, v in pairs(List) do --loops through all entries in List
        local humanoid = v:FindFirstChild("Humanoid") --Checks for a humanoid
        local hrp = v:FindFirstChild("HumanoidRootPart") --Checks for a RootPart

        if humanoid and hrp and v ~= script.Parent then --If it has both and v is not the script's parent
            local dist = (Position - hrp.Position).Magnitude 
            if dist <= Distance and humanoid.Health > 0 then
                local plr = game.Players:GetPlayerFromCharacter(v)
                if plr then --If it's a player
                    Target = hrp --we have our target!
                end
            end
        end
    end
    return Target
end


while wait() do --don't want to cause a timeout
    local target = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
    if target then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
end

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello! Here is how to fix it:

function findNearestPlayer(Position)
    wait(0.3)
    local List = game.Workspace:GetChildren()
    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) and (game.Players:GetPlayerFromCharacter(Temp2)) 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
if game.Players:GetPlayerFromCharacter(Temp) then
                    Distance = (Temp.Position - Position).magnitude
                    end
                end
            end
        end
    end
    return Torso
end

I have experience with NPC's so I hope this helps. Other than that, everything is good!

Log in to vote
-1
Answered by 4 years ago

Sorry I don't know much about scripting but I'm pretty sure the script tries to find "Humanoid" and then goes for it. Maybe try changing the NPC Humanoid's name to "NPCHumanoid" and that should fix it.

0
That's not gonna fix it. alexfinger21 341 — 4y
0
He tried to make it so it only attacks players though... Utter_Incompetence 856 — 4y

Answer this question