Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to make a "breadcrumb" system for pathfinding?

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?

5 comments

1

The breadcrumbs would need to be reached through pathfinding, defeating the purpose of having breadcrumbs as you could just pathfind to the character.

0

Store Vectors in a module, don't instantiate new objects just for their Position/CFrame attribute.

0

@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.

0

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.

0

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

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.