NPC stops chasing me after i jump to void

Hi! i have an error but i don’t know how to fix it.
The problem is when i jump to void NPC stops chasing me.

Here is the output

Script:

local zombie = script.Parent
local humanoid = zombie.Humanoid

local function findTarget()
	local players = game.Players:GetPlayers()
	local maxDistance = 530
	local nearestTarget

	for index, player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (zombie.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude

			if distance < maxDistance then
				nearestTarget = target
				maxDistance = distance
			end
		end
	end

	return nearestTarget
end

while wait(0.01) do 
	findTarget()
	local target = findTarget()

	if target and target.Humanoid.Health > 0 then
		humanoid:MoveTo(target.HumanoidRootPart.Position)
	end
end

This my first Topic, i may be bad at creating topics.

I suggest checking if the HumanoidRootPart exists, for example before calculating the distance do something like this:

if target:FindFirstChild("HumanoidRootPart") then
    -- set calculate variable and do everything else
end
1 Like

ok im going to try it. Thanks!

i tried to make like that, but still does not work.

Could you post a pic of the explorer?

1 Like

Ok i fixed it. Thanks for helping me!

1 Like

Yeah, you’ll need to do target:FindFirstChildOfClass("Humanoid") as well.

1 Like