I am currently working on Pathfinding for AI but the problem is well pathfinding is very heavy on the server and also I am having trouble with my pathfinding script in general. (I plan on having a ton of AI on the map at 1 time.)
So I had this idea, what if a player dropped so called "breadcrumbs" (aka 1,1,1 parts that are invisible) so that the AI could just follow these parts and the player would do the pathfinding for them.
The big problem with this and the part that I am having trouble thinking of in my head is the interval at which the player drops breadcrumbs. If the player has an increased walkspeed then the breadcrumbs would be further apart and this can cause bugs.
How would I go about making sure every breadcrumb is dropped the same distance from the last one even if they have increased walkspeed?
Here's a quick mock-up I made. It's not perfect but it should set you in the right direction. Server-side script.
local player = game.Workspace:WaitForChild("Cousin_Potato") local head = player.Head local breadcrumbDistance = 5 local distanceTraveled = 0 local previousTime = tick() local function placeBreadcrumb(position) local breadcrumb = Instance.new("Part") breadcrumb.Position = position breadcrumb.Anchored = true breadcrumb.CanCollide = false breadcrumb.Parent = game.Workspace end while true do local ET = tick() - previousTime previousTime = tick() distanceTraveled = distanceTraveled + ET * (head.Velocity.Magnitude) print(distanceTraveled) if distanceTraveled > breadcrumbDistance then placeBreadcrumb(head.Position) distanceTraveled = 0 end wait() end
First thing, the elapsed time is updated to determine how long it has been since the last loop.
The velocity magnitude is simply the total velocity in all directions combined. If the player is moving unimpeded, they will all add up to Humanoid.Walkspeed. We can use this to calculate the distance traveled by multiplying by the E.T. since the last time around the loop.
Finally, if the distance is greater than the breadcrumb distance, place a breadcrumb and reset the distance.
*This is an accumulative function, meaning the distance adds regardless of direction. That means if your character runs in a small circle, breadcrumbs will still be added once the distance is exceeded, even if the distance to the last breadcrumb is less than breadcrumbDistance. This isn't a tough problem so I'll let you figure that one out.
Within the if statement, rather than creating news parts, you can push the position onto an array and use that for your AI to follow.
Just for clarification, the MoveTo method does not use pathfinding, so as long as the AI can reach the first/nearest breadcrumb directly, you can forgo pathfinding.
The pathfinding service path includes information about whether the AI should walk or jump. This simple script does not so you'll have to implement your own for jumping over obstacles.
Obviously, the first two lines are for the mock-up so don't actually use that to find your player.