Need help with pth finding script

Hello all,
I cant script but have been trying to make an entity(mob that should chase you down and kill you) and i have been using the Roblox documentation guide for pathfinding within the game so it does not get lost or stuck. I have an issue tho in the guide it scripts the entity to go to a certain location and not to follow a player, I am stuck and need help if anyone has a solution please tell me it.
I have changed from the original script so instead of a player moving, a Zombie does.

Link to sight I was using: https://create.roblox.com/docs/mechanics/pathfinding

The code i wrote:
local PathfindingService = game:GetService(“PathfindingService”)
local RunService = game:GetService(“RunService”)

local path = PathfindingService:CreatePath({
Costs = {
SafeRoom = math.huge
}
})

local path = PathfindingService:CreatePath({
Costs = {
Water = 200,
Mud = 5,
Neon = math.huge
}
})

local path = PathfindingService:CreatePath()

local Zombie = game.Workspace.Zombie

local humanoid = Zombie.Humanoid

local TEST_DESTINATION = Vector3.new(-48.8, 0.5, 65.6)

local waypoints
local nextWaypointIndex
local reachedConnection
local blockedConnection

local function followPath(destination)
– Compute the path
local success, errorMessage = pcall(function()
path:ComputeAsync(Zombie.PrimaryPart.Position, destination)
end)

if success and path.Status == Enum.PathStatus.Success then
	-- Get the path waypoints
	waypoints = path:GetWaypoints()

	-- Detect if path becomes blocked
	blockedConnection = path.Blocked:Connect(function(blockedWaypointIndex)
		-- Check if the obstacle is further down the path
		if blockedWaypointIndex >= nextWaypointIndex then
			-- Stop detecting path blockage until path is re-computed
			blockedConnection:Disconnect()
			-- Call function to re-compute new path
			followPath(destination)
		end
	end)

	-- Detect when movement to next waypoint is complete
	if not reachedConnection then
		reachedConnection = humanoid.MoveToFinished:Connect(function(reached)
			if reached and nextWaypointIndex < #waypoints then
				-- Increase waypoint index and move to next waypoint
				nextWaypointIndex += 1
				humanoid:MoveTo(waypoints[nextWaypointIndex].Position)
			else
				reachedConnection:Disconnect()
				blockedConnection:Disconnect()
			end
		end)
	end

	-- Initially move to second waypoint (first waypoint is path start; skip it)
	nextWaypointIndex = 2
	humanoid:MoveTo(waypoints[nextWaypointIndex].Position)



else
	warn("Path not computed!", errorMessage)
end

end

followPath(TEST_DESTINATION)

Zombie goes to position not player:

1 Like

Set the TEST_DESTINATION to the player’s HumanoidRootPart and update it once in a while.

will this work on an online sever to go to the nearest player

You’ll have to get a table of players characters and figure out the magnitude between the player’s HumanoidRootPart and the zombie’s, then select the one with the lowest magnitude and pathfind to it.

could you tell me how to do this?

I’m afraid spoonfeeding isn’t allowed here, but I’m sure you’ll be capable of finding tutorials on yt.
E.g: How To Script NPC, walking & Attack In Roblox Studio! - YouTube

1 Like