:MoveTo not working as intended

I want the rig to move to either its waypoints or to the nearest found player.

The issue is that the rig remains in place even though it is providing the players and the rigs position indicating the rig and players existence.

Code:

-->

local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")

local Bot = {}
Bot.__index = Bot

function Bot.new(model)
	local self = setmetatable({}, Bot)
	self.Model = model
	self.Humanoid = model:FindFirstChildOfClass("Humanoid")
	self.Waypoints = workspace:FindFirstChild("Waypoints"):GetChildren()
	self.CurrentWaypointIndex = 1

	warn( self )

	return self
end

function Bot:FollowPlayerOrWaypoints()
	local nearestPlayer = self:FindNearestPlayer()

	if nearestPlayer then
		
		warn( nearestPlayer.Character.PrimaryPart.Position, "--> Bot", self.Model.HumanoidRootPart.Position )

		warn(self.Model.Humanoid:MoveTo( nearestPlayer.Character.HumanoidRootPart.Position ))
	else
		warn(self:FollowWaypoints())
	end
end

function Bot:FindNearestPlayer()
	local nearestPlayer = nil
	local shortestDistance = math.huge

	for _, player in Players:GetPlayers() do
		if player.Character and player.Character.PrimaryPart then
			local distance = (self.Model.PrimaryPart.Position - player.Character.PrimaryPart.Position).Magnitude
			if distance < shortestDistance then
				shortestDistance = distance
				nearestPlayer = player
			end
		end
	end

	return nearestPlayer
end

function Bot:FollowWaypoints()
	local waypoint = self.Waypoints[self.CurrentWaypointIndex]
	if waypoint then
		self:MoveTo(waypoint.Position)

		self.CurrentWaypointIndex = (self.CurrentWaypointIndex % #self.Waypoints) + 1
	end
end

function Bot:MoveTo(targetPosition)
	warn(self.Model.Name, self.Model.Parent)
	
	self.Model.Humanoid:MoveTo( targetPosition )
end

return Bot

Seems there was something wrong with my previous rig, after inserting a new one it worked just fine.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.