So basically I am using string value attributes to be as factions. I’m making it so that if 2 npcs have at least one matching attribute then they would not attack each other, like for example NPC 1 has factions Human and Demon and NPC 2 has factions Human and Alien then they would be allies because of the matching attribute: Human. The problem is that the for loop
isn’t going to the next instance to check them. For example, an npc and its ally are 5 studs apart so if the enemy is 10 studs away from the both of them, the script will be stuck on the ally npc and not check that there is an enemy nearby because the ally npc is the nearest to the npc. I don’t understand why the loop won’t just go forward once it realizes that the ally is an ally.
function findTarget(pos)
local target = nil
local range = 10000 - detect range
for i, v in ipairs(workspace:GetChildren()) do -- isn't going to the next 'v'
local model = v:IsA("Model") and v ~= npc
if model then
local nhum = v:FindFirstChild("Humanoid")
local nhrp = v:FindFirstChild("HumanoidRootPart")
local nfac = v:FindFirstChild("Faction")
local natt = nfac:GetAttributes()
if nhum and nhum.Health > 0 and nhrp and nfac then
if(nhrp.Position - pos).Magnitude < range then -- if distance < detect range
print(nhrp.Parent)
for i, v in pairs(faction) do
for l, j in pairs(natt) do
if i == l then
target = nil -- if both attributes match, no target(at least 1 matched).
else
target = nhrp
range = (nhrp.Position - pos).Magnitude
end
end
end
end
end
end
end
return target
end
while true do
wait()
local target = findTarget(hrp.Position)
if target then
path.Visualize = true
path:SetHipHeight(1)
path:Run(target.Position)
end
end
The NPCs are only going after the red guy if it’s closer to them than their own allies which is confusing.
Red = Enemy
Skin Color = NPCs
I don’t know how to approach an NPC having multiple factions so I’m using attributes but it may be inefficient.