Implement Flocking

  1. What do you want to achieve? Keep it simple and clear!
    I want to implement flocking into an NPC system to avoid being in clusters.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t really know how to implement it into ROBLOX Pathfinding.
  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    I’ve looked at the Forums and haven’t seen like a tutorial so far.

I just don’t know how to implement it.

In games, Steering Behaviors are usually used. it’s actually pretty easy to understand, but a bit tricky to put into practice.
You calculate three vectors: alignment, cohesion, and separation, which help adjust the NPC’s movement.
It’s all explained in the link.

I’ll look into Steering Behaviors, but were would I put them after pathfinding?

It really depends on how you’re handling pathfinding.
In a typical setup, you use the next waypoint to figure out the NPC’s velocity, apply the adjustment using the three vectors, and then calculate a new next waypoint to use with humanoid:MoveTo().

Somehow, you need to have the NPC’s velocity to make the adjustment.

Might help

Something I do to make the npcs not overlap each other is this function that checks the point in which the npc is moving to and checks if it’s occupied by another npc and adjust it accordingly
I can provide the function here.

function CheckSpace(RootP, pos)
	if not RootP or not pos then return pos end

	-- Get the primary part of the RootP
	local RootP2 = RootP:IsA("Model") or RootP:IsA("Actor") and (RootP.PrimaryPart or RootP:FindFirstChildOfClass("BasePart")) or RootP
	if not RootP2 then return pos end

	local radiusX, radiusZ = RootP2.Size.X / 2, RootP2.Size.Z / 2
	local slots = {
		pos + Vector3.new(0, 0, -radiusZ),
		pos + Vector3.new(radiusX, 0, -radiusZ),
		pos + Vector3.new(-radiusX, 0, -radiusZ),
		pos + Vector3.new(0, 0, radiusZ),
		pos + Vector3.new(-radiusX, 0, radiusZ),
		pos + Vector3.new(radiusX, 0, radiusZ),
		pos + Vector3.new(0, 0, -radiusZ * 2),
		pos + Vector3.new(radiusX * 2, 0, -radiusZ),
		pos + Vector3.new(radiusX, 0, -radiusZ * 2),
		pos + Vector3.new(-radiusX, 0, -radiusZ * 2),
		pos + Vector3.new(0, 0, radiusZ * 2),
		pos + Vector3.new(-radiusX, 0, radiusZ * 2),
		pos + Vector3.new(radiusX, 0, radiusZ * 2)
	}

	--if not aware then  aware = require(script.ChatbotAlgorithm.Awareness) end
	local enemies = workspace.Enemys:GetChildren()--aware.Gettable("enemys")
	local slot = 1

	for _, enemy in pairs(enemies) do
		local enemyRoot = enemy:FindFirstChild("HumanoidRootPart") or enemy:FindFirstChild("Head")
		if RootP2 and RootP2.Parent and enemyRoot and enemyRoot.Parent and enemyRoot ~= RootP2 and enemyRoot.Parent ~= RootP2.Parent then
			local enemyRadiusX, enemyRadiusZ = enemyRoot.Size.X / 2, enemyRoot.Size.Z / 2
			--local conflict = false

			--repeat
			for _,slotPos in slots do 
				--local slotPos = slots[slot]
				if (RootP2.Position - Vector3.new(slotPos.X, RootP2.Position.Y, slotPos.Z)).Magnitude < (radiusZ + enemyRadiusZ - 1) then
				--	conflict = true
				--	slot = slot + 1
				else
					--conflict = false
					pos = slotPos
					break
				end
			end	
			--until not conflict or slot > #slots
		end
	end

	return pos or slots[math.random(1, #slots)]
end

This function organized the npcs around the target into a formation where the first 4 are in a + formation and it can handle up to 16 overlapping npcs before returning the original position.