Monster not move to me

Hi I am making a monster that chase a player but it isn’t working now but it was work before. Here is code

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

local Zombie = script.Parent
local Humanoid = Zombie.Humanoid
local HumanoidRootPart = Zombie.HumanoidRootPart

local function findClosestPlayer()
	local closestPlayer = nil
	local closestDistance = 120

	for _, player in ipairs(Players:GetPlayers()) do
		local character = player.Character
		if character and character:FindFirstChild("Torso") then
			local distance = (HumanoidRootPart.Position - character.Head.Position).magnitude
			if distance < closestDistance then
				closestPlayer = character
				closestDistance = distance
			end
		end
	end

	return closestPlayer
end

local function followPath(destination)
	local path = PathfindingService:CreatePath()
	path:ComputeAsync(HumanoidRootPart.Position, destination)

	if path.Status == Enum.PathStatus.Success then
		local waypoints = path:GetWaypoints()
		HumanoidRootPart:SetNetworkOwner(nil)

		for i, waypoint in pairs(waypoints) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				Humanoid.Jump = true
			end
			Humanoid:MoveTo(waypoint.Position)
		end
	end
end

local function updatePath()
	local closestPlayer = findClosestPlayer()
	if closestPlayer then
		followPath(closestPlayer.Head.Position)
	end
end

RunService.Heartbeat:Connect(updatePath)

I try a lot of things but nothing worked so I’m glad if somebody helped me

2 Likes

please relocate this post to #help-and-feedback:scripting-support before the mini mods appear.

OK I will do that now

I managed to fix the issue that made the zombie not chase you. It’s from findClosestPlayer(), and the issue was character:FindFirstChild("Torso")

The player doesn’t have anything called Torso (just “UpperTorso”), but it would probably be better to get the root part. It’s what I did, but if you’d like the latter, just replace “Torso” with “UpperTorso”

Here’s the fixed code for you:

local function findClosestPlayer()
	local closestPlayer = nil
	local closestDistance = 120

	for _, player in ipairs(Players:GetPlayers()) do
		local character = player.Character
		if character then
			local target_root = character.PrimaryPart
			if target_root then
				local distance = (HumanoidRootPart.Position - target_root.Position).magnitude
				if distance < closestDistance then
					closestPlayer = character
					closestDistance = distance
				end
			end
		end
	end

	return closestPlayer
end

This topic has been solved

Only reply here if:

  • You have additional details

  • The solution doesn’t work for you

If you have an unrelated issue, please start a new topic instead.
Remember, dont feed the trolls

Why are you stalking me? This is a post I need help with.

But now it is solved OK. You know

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