Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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!

3 comments

0

wait where in the script do i put this tho

0

nvm i figured it out thx for the help!

0

No problem!

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

1 comment

0

i thought about that but then i was still skeptical but i guess i can try

This discussion is preserved as it was. New answers, comments, and votes are closed.