Zombie AI | Not Detecting Players

Hi! I am trying to make my zombie in my game go after players all the time. Also goes after the same player if in range.

I have tried to fix this but I am still at a loss.

I have looked already but no solutions answer my problem currently.

[quote=“greeninja_97, post:1, topic:1837847, full:true”]
Hi! I am trying to make my zombie in my game go after players all the time. Also goes after the same player if in range.

I have tried to fix this but I am still at a loss.

I have looked already but no solutions answer my problem currently.

SCRIPT:

wait()
local hum = script.Parent:FindFirstChild("Humanoid")
local torso = script.Parent:FindFirstChild("HumanoidRootPart")

local max_dist = 200

function findNearestTorso()
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Character ~= nil then
			local target_torso = player.Character:FindFirstChild("HumanoidRootPart")
			if target_torso ~= nil then
				if (target_torso.Position - torso.Position).magnitude < max_dist then
					return target_torso
				end
			end
		end
	end
end

local old_pos = torso.Position

wait(math.random(0, 90)/100)

while hum ~= nil do
	local target = findNearestTorso()
	if target ~= nil then
		while target ~= nil and target.Parent ~= nil and hum ~= nil do
			if (target.Position - torso.Position).magnitude < max_dist then
				hum:MoveTo(target.Position, target)
				if target.Position.Y > torso.Position.Y + 3 then
					hum.Jump = true
				end
				if (old_pos - torso.Position).magnitude < 0.5 then
					if math.random(1, 3) == 1 then
						torso.Velocity = torso.CFrame.lookVector * -40
						hum:MoveTo(torso.Position + Vector3.new(math.random(-1, 1), 0, math.random(-1, 1))*20)
						wait(1.2)
					else
						--hum.Jump = true
						hum:MoveTo(target.Position, target)
					end
				end
			else break end
			old_pos = torso.Position
			wait(0.5)
		end
	end
	wait(4)
end

If you can help me please do! Thanks!

1 Like

Here’s a little section of my pathfinding script

local function findTarget()
	local players = game:GetService("Players"):GetPlayers()
	local nearesttarget
	local maxDistance = 5000 -- distance

	for i,player in pairs(players) do
		if player.Character then
			local target = player.Character
			local distance = (npc.HumanoidRootPart.Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude

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

		end
	end
	return nearesttarget
end

This should work

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 10000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end




while true do
	wait(1)
	local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
	if target ~= nil then
		script.Parent.Zombie:MoveTo(target.Position, target)
	end

end

It goes to the player but doesnt use the animation, also it teleports instead of walks

Your using :MoveTo() on a model and not the actual humanoid. Try script.Parent.Zombie.Humanoid:MoveTo(target.Position, target)

Did you load the animation?

When the zombie first spawns, it uses the animation. When it respawns it doesn’t.

You could try loading the animation at the top of the script so it is always there.

local GetExampleAnim= script.Parent.Animations.ExampleAnim

local ExampleAnim= script.Parent.Humanoid:LoadAnimation(GetExampleAnim)

When you want to load the animation, just call. Try and put this function below in the last while loop.

ExampleAnim:Play()