You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Keep it simple and clear!
Make a hoard of npcs follow me -
What is the issue? Include screenshots / videos if possible!
Ive managed to do some digging and found my way into pathfinding, but some npcs dont seem to follow - What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i dont really have any knowledge on pathfinding so i cant really remake the script
Here is the script: (Its located in ReplicatedStorage, cloned into each NPC btw)
local NPC = script.Parent
local pathfinding_service = game:GetService("PathfindingService")
function getClosestPlayer()
local closest_player, closest_distance = nil, 200
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and player ~= NPC and game.Players:FindFirstChild(player.Name) then
local distance = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < closest_distance then
closest_player = player
closest_distance = distance
end
end
end
return closest_player, closest_distance
end
while true do
local path = pathfinding_service:CreatePath()
local player, distance = getClosestPlayer()
if player and distance > 10 then
path:ComputeAsync(NPC.HumanoidRootPart.Position, player.PrimaryPart.Position)
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
NPC.Humanoid:MoveTo(waypoint.Position)
while true do
local distance = (NPC.PrimaryPart.Position - waypoint.Position).Magnitude
local distance_from_player = (NPC.PrimaryPart.Position - player.PrimaryPart.Position).Magnitude
if distance < 6 then
print('too close from player')
break
end
if distance_from_player < 10 then
NPC.Humanoid:MoveTo((NPC.HumanoidRootPart.CFrame*CFrame.new(0,0,-3)).p)
break
end
wait()
end
end
end
wait()
end