I try to make a script for pathfinding but it doesn't work. It says "MoveTo() is not a valid member Path "Instance"" Is it because MoveTo() doesn't work and more importantly, what alternatives do I have?
local PathFindingService = game:GetService("PathfindingService") local Agent = game.Workspace.StarterCharacter.Humanoid AgentRadius = 2 -- Settings Agent AgentHeight = 5 AgentCanJump = false AgentCanClimb = false AgentSlope = 10 AgentMaxAcceleration = 1000 AgentMaxSpeed = 16 AgentArrivalDistance = 1 -- end settings agent print(type(PathFindingService)) while true do if Agent then local destination= Vector3.new(math.random(-571,165),5.5,math.random(-495,361)) print(true) if destination then local path = PathFindingService:CreatePath() if path then print(true) start = Agent.HumanoidRootPart.Position if start then path:ComputeAsync(start, destination) path:MoveTo(Agent) --error else print (false) end end end end wait(5) end
You have to use the MoveTo
function on the agent humanoid not on the path variable. E.g Agent:MoveTo(Position)
Edit:
Instead of passing passing the path instance you have to loop through the points by calling the GetWayPoints()
method on the path instance.
local PathFindingService = game:GetService("PathfindingService")
local Agent = script.Parent
AgentRadius = 2 -- Settings Agent
AgentHeight = 5
AgentCanJump = false
AgentCanClimb = false
AgentSlope = 10
AgentMaxAcceleration = 1000
AgentMaxSpeed = 16
AgentArrivalDistance = 1 -- end settings agent
print(type(PathFindingService))
while true do
if Agent~= nil then
local destination= Vector3.new(math.random(-40,40), -8, math.random(-40, 40))
print(true)
local path = PathFindingService:CreatePath()
local waypoints = path:ComputeAsync(Agent.HumanoidRootPart.Position,destination)
if path.Status == Enum.PathStatus.Success then
local waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
Agent.Humanoid:MoveTo(waypoint.Position)
--print(waypoint.Position)
end
end
end
wait(5)
end