Wandering NPC only moving sometimes

Hello! Im currently writing a randomly wandering npc, and it only follows the goal sometimes
Code:

local PathFinding = game:GetService("PathfindingService")
local UIS = game:GetService("UserInputService")

local destination -- make sure to keep as a vector3 value (postion, vector3.new, etc)

local goal = Instance.new("Part")

goal.Parent = workspace
goal.Name = "Goal"

local pathParams = {
	200, -- agentRadius
	5, -- agentHieght
	true -- agentCanJump
}

while wait(5) do
	destination = Vector3.new(math.random(5,20), math.random(5,20), math.random(5,20))
	print(destination)
	goal.Position = destination
	goal.Parent = workspace
	goal.Name = "Goal"
	
	local path = PathFinding:CreatePath()

	local humanoid = script.Parent:FindFirstChild("Humanoid")

	path:ComputeAsync(script.Parent.HumanoidRootPart.Position, goal.Position)

	local waypoints = path:GetWaypoints(goal)

	for _, waypoint in pairs(waypoints) do
		local part = Instance.new("Part")
		part.Shape = "Ball"
		part.Material = "Neon"
		part.Size = Vector3.new(0.6, 0.6, 0.6)
		part.Position = waypoint.Position
		part.Anchored = true
		part.CanCollide = false
		part.Parent = game.Workspace

		humanoid:MoveTo(waypoint.Position)

		humanoid.MoveToFinished:Wait()		
	end

end

pathfindingDummy.rbxm (7.9 KB)

I am away from my computer, and it is late but I’ll check this out within the next 12 hours.

You may consider using Polaris-Nav, which is going into testing next week. It is a new pathfinder that comes with a controller to follow the generated paths. Here is the server: Polaris-Nav

1 Like

Are you sure this isn’t due to the fact that the NPC can’t reach some certain places?

its on a flat baseplate, ive seen it work, then not work fior the next 5 regens, then work

@IGOTHISLOL is right,

The goal y is in range [5, 20], meaning that if the baseplate it flat then only some y values are reachable by the pathfinder. If it is too high or below the baseplate then a path will not be found. Set the y value to 2-3 studs above your baseplate.

In addition, the pathParams are not being passed into :CreatePath(). The pathParams also needs to be a dictionary, not an array, like so:

local pathParams = {
    AgentRadius = 2;
    AgentHeight = 5;
    AgentCanJump = true;
}

pairs() is being used to iterate over the waypoints instead of ipairs(). While this may work in this case, it depends on the internals creating the waypoints table if pairs actually iterates over the array in order. It is not guaranteed by Lua, thus you should use ipairs() instead which is guaranteed to iterate in order, and only over array keys.

The goal is not anchored, meaning that when it generates above it falls to the baseplate and seems like the goal was on the baseplate all along (probably why you didn’t “catch” the height problem)

goal.Parent and goal.Name are being set multiple times

Destination is only set / used in the while loop, but defined as an upvalue

FindFirstChild is being used to get the Humanoid, but is not being checked for existence

4 Likes

If you dont mind elaborating this, im not entirly sure what you mean

1 Like

Absolutely!

Since you cannot index an instance with the name of a child that does not exist without an error being thrown, FindFirstChild was introduced to check if a child exists and if it does, get it. Since it’s presence indicates that what you are looking for may not exist, its result should always be checked to ensure it is not nil.

The fact that it isn’t checked here, but that it exists in your code, tells me that part of your code was copied from elsewhere and it was not fully understood, or understood but effort wasn’t given to ensure correctness. I assumed the first, so brought it to your attention. In general, this is one of the signs of someone who is in the process of learning.

Best wishes on your journey! You seem pretty far along and have a bright mind. I have no doubt you’ll figure it all out and become a great scripter.

2 Likes