Force NPCs to avoid each other

Hi. Here I am again, after many hours of stress of trying to make NPCs avoid each other. I made many solutions but they all didn’t work, which is why I am here.

How can I do this effectively so that they avoid each other most of the time? Please leave some ideas below because I am getting really stressed and desperate. Thanks for your help!

There are many ways to approach this, you could do it by name or add values inside to indicate their “team.” That is what I would do when programming NPC’s, I usually add some Value with the name of their “team” and use an if statement to check for that value. Here is an example:

NPC (Model) > Bot (BoolValue)

if not target.Parent:FindFirstChild("Bot") then -- target would just be the basepart of the character the NPC wants to chase after
    -- code here
end

To avoid eachother you just check for a value, or attribute for the bot like @CapnFry said.

Hi. Sorry for the late reply.

I already have my NPCs grouped together in a group, where they know all of the people in the group so they don’t really need a value to keep track if they are a bot. To me, it doesn’t matter if groups collide with groups. But, I like the idea that you brought up. I will raycast, then check if it is part of the group, then avoid it.

PathfindingService basically finds a route around objects; it’s fairly complex but I reccomend using it.

Ok. I will try using that service.

It’ll avoid objects in it’s path essentially instantly. It’s better then using math to get a assumption for where it needs to go back.

Wow, but I have a question. Will it take a lot of resources? I am running my thing on Heartbeat.

I’ve recently made something like this. All it does is raycast in the lookvector of the HumanoidRootPart.

As long as nothing is there I generally don’t do anything. But if there is something blocking then I do a generic for loop in increments of a specific angle. For instance

for i=-45, 45, 15 do
 -- loop between -45 degrees and 45 degrees by 15 degree increments.
end

I then check to see if there’s a free angle. If so then I make them walk in that direction for x amount of seconds. Then they go back to walking forward.

https://i.gyazo.com/61c27b32fe7d45a08f51e749162a24c7.gif

It’s not perfect. But it does work. This is an earlier version. It’s definitely resource-hungry but as you can see it works kind of okay with a large number. In my experience, the delays for walking back in a straight line gets longer and longer. I’ve tried using parallel Lua but unfortunately, it caused crashes frequently.

1 Like

Thanks for your reply.

Because others’ solutions haven’t worked the way I hoped, I will try your solution because it seems the simplest out of them all.

EDIT: I also have a question. How do you fire rays at a certain angle? (sorry i’m still in algebra 2)

I simply grab the CFrame from the HumanoidRootPart and then multiply it by the angle.

HumanoidRootPart.CFrame * CFrame.Angles(0, math.rad(i), 0)

Wow, why didn’t I think of that? Anyways, thanks for the solution.

1 Like

Hi everyone. I am here to present an update. Here’s a video of what happened thanks to this topic.

As you can see, it works! A big part of this success came from @T0ny, who helped me with the process for it.

reply for solution

Thank you everyone for replying and trying to help me out!

1 Like