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

How do i get the npcs to NOT attack each other?

Asked by 4 years ago

My npcs both have follow scripts but when they are next to each other they go for each other and even when i get close they still wont attack me can somebody please help


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 = 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 - 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,2)) local target = findNearestTorso(script.Parent.HumanoidRootPart.Position) if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position, target) end end

2 answers

Log in to vote
1
Answered by 4 years ago

I think a way to fix this is to put a Value or distinct object inside of the npc, then have the script check if they are a npc or not, that way they can be named different things. Example:

A boolvalue that is true called NPC inside of the... NPC. Then in the script:

while true do
    wait(math.random(1,2))
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
    local npc = target:FindFirstChild("NPC", true) --[[The part I added]] 
    if npc == nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end
    end

end

Hope this helps!

0
wait where in the script do i put this tho coolmanHDMI 72 — 4y
0
nvm i figured it out thx for the help! coolmanHDMI 72 — 4y
0
No problem! fighterkirbyzx 102 — 4y
Ad
Log in to vote
0
Answered by
quinzt 201 Moderation Voter
4 years ago

try to change the name of the humanoids inside of your npcs, and change all the scripts inside of them accordingly

0
i thought about that but then i was still skeptical but i guess i can try coolmanHDMI 72 — 4y

Answer this question