How would I do Humanoid:MoveTo() and the position argument would be the first waypoint position of a pathfinding system ?
I’ve do this but the character won’t move at all :
local PathfindingService = game:GetService("PathfindingService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local Left = workspace:WaitForChild("Left"..Character.Name)
local Right = workspace:WaitForChild("Right"..Character.Name)
local Path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false
})
while true do
local CharacterPosition = Character.PrimaryPart.Position
local LeftPosition = Left.Position
local RightPosition = Right.Position
local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
if LeftMagnitude < RightMagnitude then
Path:ComputeAsync(CharacterPosition, LeftPosition)
local PathWaypoints = Path:GetWaypoints()
Humanoid:MoveTo(PathWaypoints[1].Position)
Humanoid.MoveToFinished:Wait()
elseif LeftMagnitude > RightMagnitude then
Path:ComputeAsync(CharacterPosition, RightPosition)
local PathWaypoints = Path:GetWaypoints()
Humanoid:MoveTo(PathWaypoints[1].Position)
Humanoid.MoveToFinished:Wait()
end
end
First index points to the current location of the character. Use the second point of the generated path.
This code though seems to be bad use of the pathfinder. You can just keep using the same path and increment until the last waypoint is reached. If the path has been blocked by an object however, generate a new path there and repeat the previous process. The Creator Documentation has a tutorial made for this.
There are 2 issues with the code you are showing that may be causing the character not to move:
1. The MoveTo function takes a Vector3 position as an argument, not a list of waypoints. You should pass the final destination position to MoveTo , not the first waypoint in the path. 2. The MoveToFinished event will only be fired once the character has reached the destination position. If the character is not moving at all, this event will never be fired, and the script will become stuck in an infinite loop waiting for the event to be triggered.
To fix these issues, you can change your code to pass the final destination position to MoveTo , and use a boolean flag to track whether the character has reached the destination or not. Here is an example of how you could do this:
local PathfindingService = game:GetService("PathfindingService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local Left = workspace:WaitForChild("Left"..Character.Name)
local Right = workspace:WaitForChild("Right"..Character.Name)
local Path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false
})
while true do
local CharacterPosition = Character.PrimaryPart.Position
local LeftPosition = Left.Position
local RightPosition = Right.Position
local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
local destinationPosition = nil
if LeftMagnitude < RightMagnitude then
destinationPosition = LeftPosition
elseif LeftMagnitude > RightMagnitude then
destinationPosition = RightPosition
end
if destinationPosition then
Humanoid:MoveTo(destinationPosition)
while Humanoid.Movement.Velocity.Magnitude > 0 do
wait()
end
end
end
OP asks for MoveTo, not pathfinding, but here’s a way it might work with pathfinding:
local PathfindingService = game:GetService("PathfindingService")
local Character = script.Parent
local Humanoid = Character.Humanoid
local Left = workspace:WaitForChild("Left"..Character.Name)
local Right = workspace:WaitForChild("Right"..Character.Name)
local Path = PathfindingService:CreatePath({
AgentRadius = 2,
AgentHeight = 5,
AgentCanJump = false
})
while true do
local CharacterPosition = Character.PrimaryPart.Position
local LeftPosition = Left.Position
local RightPosition = Right.Position
local LeftMagnitude = (CharacterPosition - LeftPosition).Magnitude
local RightMagnitude = (CharacterPosition - RightPosition).Magnitude
local destinationPosition = nil
if LeftMagnitude < RightMagnitude then
destinationPosition = LeftPosition
elseif LeftMagnitude > RightMagnitude then
destinationPosition = RightPosition
end
if destinationPosition then
Path:ComputeAsync(CharacterPosition, destinationPosition)
local PathWaypoints = Path:GetWaypoints()
Humanoid:MoveTo(PathWaypoints[1].Position)
while Humanoid.Movement.Velocity.Magnitude > 0 do
wait()
end
end
end
Thanks you very much @Y_VRN and @Arylist for your replies !
But I have found the problem a few minutes after I posted, and the problem was I used the first waypoint but that doesn’t work with that one you need to use the second !