Pathfinding not working

The zombie just stays still no matter what changes i make to the script

local pathfindingService = game:GetService("PathfindingService")
local zombie = script.Parent
local HumanoidRootPart = zombie:WaitForChild("HumanoidRootPart")
local Humanoid = zombie:WaitForChild("Humanoid")
local Players = game:GetService("Players")


for i, v in pairs(zombie:GetChildren()) do
	v.Anchored = false
end

function getNearestCharacterToPart(part)
	local currentPlayer = nil
	local distanceToNearest = math.huge
	for _, player in Players:GetPlayers() do
		local char = player.Character
		local hrp = if char then char:FindFirstChild("HumanoidRootPart") else nil
		if hrp then
			local dist = (hrp.Position - part.Position).Magnitude
			if dist < distanceToNearest then
				currentPlayer = player
				distanceToNearest = dist
			end
		end
	end
	return currentPlayer
end

local nearestPlayer = getNearestCharacterToPart(HumanoidRootPart)


game:GetService("RunService").Heartbeat:Connect(function()
	if nearestPlayer then
		local path = pathfindingService:CreatePath()
		local success, errorMessage = pcall(function()
			path:ComputeAsync(HumanoidRootPart.Position, nearestPlayer.Character.PrimaryPart.Position)
		end)
		if success then
			local waypoints = path:GetWaypoints()
			for i, v in pairs(waypoints) do
				Humanoid:MoveTo(v.Position)
			end
		end
		if not success then
			warn(errorMessage)
		end
	end
	Humanoid.MoveToFinished:Wait()
end)


Do you use local script or serverside script? If it’s a serverside then it wont prob work.

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