Making a zombie find a humanoid to hunt

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Make a zombie find a player torso to hunt EXPLAIN HOW

  2. What is the issue? Include screenshots / videos if possible!
    can’t figure out how

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    yes
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Hi!

I’ve answered a post with a simple NPC pathfinding script that you may find useful for the start. It’s not written optimally, but works well. Might have to add jumping over obstacles (I’ll ping you after I update the script). How use it? Insert R6 or R15 dummy, unanchor HumanoidRootPart and put this script inside NPC humanoid.

Script with pathfinding
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local PathfindingService = game:GetService("PathfindingService")

local humanoid = script.Parent
local hrp = script.Parent.Parent:FindFirstChild("HumanoidRootPart")

local find_path = false
local playerRootPart

local function checkForMovement()
	while (true) do
		if (humanoid.MoveDirection:Dot(humanoid.MoveDirection) == 0) then
			wait(.5)
			if (humanoid.MoveDirection:Dot(humanoid.MoveDirection) == 0) then
				find_path = true
			end
		end
		wait(3)
	end
end

local function chasePlayer(player)
	if (not playerRootPart) then return; end
	coroutine.wrap(checkForMovement)()
	while (true) do
		if (not find_path) then
			humanoid:MoveTo(playerRootPart.Position)
		else
			local path = PathfindingService:ComputeRawPathAsync(hrp.Position, playerRootPart.Position, 512)
			local points = path:GetPointCoordinates()

			for p = 3, #points do
				humanoid:MoveTo(points[p])
				humanoid.MoveToFinished:Wait()
			end
			find_path = false
		end
		RunService.Heartbeat:Wait()
	end
end

Players.PlayerAdded:Connect(function(player)
	if (not player.Character) then player.CharacterAdded:Wait() end
	playerRootPart = player.Character:WaitForChild("HumanoidRootPart")
	chasePlayer(player)	
end)

To learn more about pathfinding, check this Roblox Dev Hub page:

EDITED (2021-03-08)

@Mr_LegendarySword I wrote you a basic and not very smart zombie that can damage the player and jump over obstacles. Same as the script above, belongs in humanoid of NPC. Make sure HumanoidRootPart is unanchored.

local Players = game:GetService("Players")

local humanoid = script.Parent
local hrp = script.Parent.Parent:FindFirstChild("HumanoidRootPart")

local playerRootPart, magnitude, nearest

local function lookAround()
	magnitude = 100
	for _, player in pairs(Players:GetPlayers()) do
		if (not player.Character) then continue; end
		local rootPart = player.Character:FindFirstChild("HumanoidRootPart")
		if (rootPart) then
			local dist = (hrp.CFrame.Position - player.Character.HumanoidRootPart.CFrame.Position).Magnitude
			-- Compare magnitudes and find the nearest part.
			magnitude = magnitude and math.min(magnitude, dist)
			nearest = rootPart
		end
	end
	playerRootPart = nearest
	return magnitude
end

coroutine.wrap(function()
	while (true) do
		if (humanoid.MoveDirection.Magnitude == 0) then
			wait(0.25)
			if (humanoid.MoveDirection.Magnitude == 0) then
				hrp.AssemblyLinearVelocity = Vector3.new(0, 50, 0) -- jump
			end
		end
		wait(1.5)
	end
end)()

while (true) do
	magnitude = lookAround()
	if (magnitude < 25) then
		repeat
			magnitude = (hrp.Position - playerRootPart.Position).Magnitude
			game:GetService("RunService").Heartbeat:Wait()
			if (playerRootPart and magnitude) then
				humanoid:MoveTo(playerRootPart.CFrame.Position)
			end
			if (magnitude < 5) then playerRootPart.Parent.Humanoid.Health -= .3 end
		until (magnitude > 25 or not playerRootPart)
	end
	playerRootPart = nil
	wait(2)
end

You can add animations and take this as a basic example. I suggest taking a look at this NPC first and later, should you decide to, move to Roblox zombie.

1 Like

I would suggest having a look at this free Zombie from Roblox:

Download it and have a look through the script inside as it has a feature that you can use/learn from for searching and chasing humanoids

I don’t want to use path finding because zombies are dumb. What I wanted is a way a zombie can detect if a player is near and attack it.

:MoveTo & :FindFirstChild("Humanoid") of a player

If you want a humanoid to randomly search for a player to “hunt”, use a simple raycast and magnitude check that, if there’s some obstruction, keep walking to the last seen location. Pathfinding service should actually help majority of the time, otherwise, if brave enough, you can write your own algorithm. Hehheh.

Movement of the zombie should be using the script to fire functions from the Humanoid object.

Yes, I know but how does it work example please.