Custom NPC System & Pathfinding Issues

I’m trying to make a simple pathfinding humanoid walk over to a part.

I’ve tried many things, when I used humanoid.MoveToFinished:Wait() inbetween waypoints, it would stop and start a bit cause it walks just past the target point.

Here’s whats in the ModuleScript making a “Path” class:

function Path.new(humanoid,startPosition,endPosition,agentParamaters)
	local self = setmetatable({}, Path);

	local path = pathfindingService:CreatePath()
	
	self.pathObject = path
	self.humanoid = humanoid
	self.humanoidRootPart = humanoid.Parent["HumanoidRootPart"]
	self:Compute(startPosition,endPosition,agentParamaters)
	
	local waypoints = path:GetWaypoints()
	self.waypoints = waypoints

	return self;
end

function Path:Start()
	local waypoints = self.waypoints
	local humanoid = self.humanoid
	
	local reachedGoal = true
	
	self.Continue = true
	
	for index,point in pairs(waypoints) do
		if self.Continue == false then reachedGoal = false break end
		self.currentIndex = index
		
		humanoid:MoveTo(point.Position)
		
		if point.Action == Enum.PathWaypointAction.Jump then
			humanoid.Jump = true
		end
		
		local i = 0
		repeat
			i = i + 1
			local closeEnough = math.abs((self.humanoidRootPart.Position-point.Position).Magnitude) <= 0.1
			if i%20 == 0 then wait() end
		until closeEnough -- I'm using this proximity method because .MoveToFinished:Wait() was a bit jittery.
	end
	
	return reachedGoal
end

function Path:Stop()
	self.Continue = false
	pcall(function() self.humanoid:WalkTo(self.humanoidRootPart.Position) end)
end

function Path:Compute(startPosition,endPosition,agentParamaters)
	self.endGoal = endPosition
	self.pathObject:ComputeAsync(startPosition,endPosition)
end

And here’s what I’m using on the client to make and run a path:

while not game:IsLoaded() do wait() end

wait(5)

local pathModule = require(game.ReplicatedStorage.ReplicatedModules.PathClass)
local humanoid = game.Workspace:WaitForChild("noob"):WaitForChild("Humanoid")

local path = pathModule.new(humanoid,humanoid.Parent.HumanoidRootPart.Position,game.Workspace.endpart.Position)

warn("Starting pathfinding")
local worked = path:Start()
print("ended pathfinding",worked)

(Picture of noob and the target part.)

Also, humanoid’s WalkSpeed, HipHeight are set. Nothing but the target part and the ground are anchored.

[EDIT:] My apologies, forgot to mention what was wrong. The humanoid just doesnt move at all, or after 20+ seconds suddenly walks to one of the points.

I’m open to all suggestions and this is my first devforum post so feedback would be great!

2 Likes

I am assuming that agentParameters are declared somewhere.

Also, as for this, self does not have a function called Compute. That is located inside your Path class.

self:Compute(startPosition,endPosition,agentParamaters)

Try

Path:Compute(startPosition,endPosition,agentParamaters)

Also note that inside Path:Compute(), self does not refer to the self defined in Path.new (where you set the metatable), but it refers to Path. Therefore, self.pathObject will not exist, since it is looking inside Path for it.

To pass in your self table that you made, do Path.Compute(self, startPosition, endPosition, agentParameters).

There are more issues to related to self that I have not typed. Double check the selfs in your code, and let let me know if you need more help.

1 Like

Thank you man, I’ll fix it when I get back on. First time using self and metatables so I’m trying lol.