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.
5 comments
The breadcrumbs would need to be reached through pathfinding, defeating the purpose of having breadcrumbs as you could just pathfind to the character.
Store Vectors in a module, don't instantiate new objects just for their Position/CFrame attribute.
@Goulstem thats a good idea too but how do I figure out how often to store like what numbers do I divide. The math part is the part im having problems with.
Although this is a clever idea, it's not very practical, for instance, even if you took into account that a walkspeed of 16 moves your player 16 studs every second (only if constantly moving), you would also need to factor in the client's ping to account for lag, and you would also need to account for if they are moving up/down a slope, or falling through the air.
continued because comment length limit: If you still wanted to go through will all of that, you could start calculating the rate of which you drop your breadcrumbs based off the fact that a walkspeed of 16 moves you 16 studs per second. I still recommend jut using path finding though