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!
