Make model chase a player with pathfinding

We are trying to make a model follow a player with pathfinding. There are obstacles that the model has to go around.

We know how to do pathfinding, but not how to make the player the target of the model.

We have looked on the developer hub and forum but, have only found how to make a rig chase the player.

3 Likes

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
4 Likes

@SwagMasterAndrew

got an error. “attempt to index nil with character”
maybe it was how i referenced the player.
How would you reference the player?

I think you may have to write a WaitForChild() function somewhere. That, or you can add a wait(3) at the top of the script. Maybe then it might work?

how would you reference the player?

no that was not it got the same error

pretty sure its how i referenced the player

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

Now i am not getting an error but m getting a warning.
"infinite yield possible on 'Workspace:WaitForChild(“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)()
2 Likes