Set the âTargetâ of the Pathfinding to be the Position of the player. The HumanoidRootPart instance of a playerâs Character might be a good part to use the position from.
Example Code:
local Player = ... --// reference to the target player
local Model = ... --// reference to your model
local Character = Player.Character
local ModelHumanoid = Model.Humanoid --// assuming your Model is rigged similarly to how roblox characters are set up.
local PlayerHumanoidRootPart = Character.HumanoidRootPart
local TargetPosition = PlayerHumanoidRootPart.Position
local ModelPosition = ... --// Define the position of your model some how, or similarly.
local Agent = PathfindingService:CreatePath() --// New Agent for pathfinding
Agent:ComputeAsync(ModelPosition, TargetPosition) --// Compute the path
local Waypoints = Agent:GetWaypoints() --// Get current waypoints
for _, Waypoint in pairs(Waypoints) do
ModelHumanoid:MoveTo(Waypoint) --// Walk to the waypoint
ModelHumanoid.MoveToFinished:Wait() --// Wait until they finish walking
end
Unfortunately, I donât have access to studio right now, so I canât be sure, but if you want to reference the player that has entered the game, you may have to make the target position:
local game.Workspace:WaitForChild(âTorsoâ)
This (I think) will reference the playerâs torso
This is pretty old, but I made a script using @DrKittyWafflesâs code.
local PathfindingService = game:GetService('PathfindingService')
local Distance = 50
local Model = script.Parent
local ModelHumanoid = Model.Humanoid
local ModelRootPart = Model.PrimaryPart
function findNearestChar()
local List = workspace:GetDescendants()
local NearestChar
for i, char in List do
if char:IsA('Model') and game.Players:GetPlayerFromCharacter(char) then
if (char.PrimaryPart.Position - ModelRootPart.Position).Magnitude <= Distance then
NearestChar = char
end
end
end
return NearestChar
end
function pathfindCompute()
while true do
local nearestChar = findNearestChar()
if nearestChar then
local PlayerRootPart = nearestChar.PrimaryPart
local TargetPos = PlayerRootPart.Position
local ModelPos = ModelRootPart.Position
local Agent = PathfindingService:CreatePath()
Agent:ComputeAsync(ModelPos, TargetPos)
local Waypoints = Agent:GetWaypoints()
for _, waypoint in pairs(Waypoints) do
ModelHumanoid:MoveTo(waypoint.Position)
ModelHumanoid.MoveToFinished:Wait()
end
end
wait()
end
end
coroutine.wrap(pathfindCompute)()