Zombie pathfinding

I am working on a zombie AI script and it isn’t working. I dont get any errors in the output it just doesnt work (zombie doesnt move)

Script:

local players = game.Players
local pathfinding = game:GetService("PathfindingService")
local collectionService = game:GetService("CollectionService")

local function getPath(origin, root)
	local rx = math.random(origin.Value.X - 25, origin.Value.X + 25)
	local rz = math.random(origin.Value.Z - 25, origin.Value.Z + 25)
	local randomV3 = Vector3.new(rx, origin.Value.Y, rz)

	local path = pathfinding:CreatePath()
	path:ComputeAsync(root.Position, randomV3)
	
	return path
end

local function moveTo(origin, root, human)
	local path = getPath(origin, root)
	
	for _, wp in pairs(path:GetWaypoints()) do
		human:MoveTo(wp.Position)
		human.MoveToFinished:Wait()
	end
end

while true do
	task.wait(5)

	for _, zombie in pairs(collectionService:GetTagged("Zombie")) do
		local human = zombie:WaitForChild("Humanoid")
		local root = zombie:WaitForChild("HumanoidRootPart")
		local origin = zombie:WaitForChild("Origin")
		
		moveTo(origin, root, human)
	end
	
end
4 Likes

A few questions:

  1. Is it computing a path? You can check this via print(#path:GetWaypoints())
  2. Is it getting the tagged zombies? I’m unfamiliar with how the CollectionService works so I am unaware of how to check this.
  3. Are you supposed to give CreatePath some values? The DevHub has some code that’s given to it, but I’m not 100%. PathfindingService | Documentation - Roblox Creator Hub

I used the print(#path:GetWaypoints()), and it results in 0, so it isn’t computing a path for some reason

My initial guess would be that the problems lies in Origin. What are the coordinates for it? What is Origin? Slight critique, but you could simplify the randomness with the below.

local rx = origin.Value.X + math.random(-25, 25)
local yz = origin.Value.Z + math.random(-25, 25)
local randomV3 = Vector3.new(rx, origin.Value.Y, rz)

Makes it more readable imo.

EDIT
I would try adding that table stuff to your CreatePath to see if it makes a difference.

local path = pathfinding:CreatePath({
  AgentRadius = 3,
  AgentHeight = 6,
  AgentCanJump = false,
  Costs = { }
})

Origin is a Vector3Value in the zombie model. I have another script inside it that sets the value:

script.Parent.Value = script.Parent.Parent:WaitForChild("HumanoidRootPart").Position
1 Like

Is it meant to make the zombie walk in a random pattern?

Yes it is, so the zombie goes around randomly in a certain area

I see. I would recommend seeing if it’s detect any zombies in the first place.

	for _, zombie in pairs(collectionService:GetTagged("Zombie")) do
		print(zombie.Name, "was found!")
		local human = zombie:WaitForChild("Humanoid")
		local root = zombie:WaitForChild("HumanoidRootPart")
		local origin = zombie:WaitForChild("Origin")
		
		moveTo(origin, root, human)
	end

Yes, it is detecting the zombie

I see. Perhaps then it’s an issue with it computing a path from point A to point B (current position to X + Z offset from Origin). What happens if you hardcode the position instead of using a Vector3Value?

local origins = {}

...

	for _, zombie in pairs(collectionService:GetTagged("Zombie")) do
		local human = zombie:WaitForChild("Humanoid")
		local root = zombie:WaitForChild("HumanoidRootPart")
		origins[zombie] = origins[zombie] or root.Position
		
		moveTo(origins[zombie], root, human)
	end

Are the zombies anchored? I’m not a scripting guy so that’s all I can think of, lol.

I doubt it. It should still attempt to compute a path even if the object was anchored.

I just found this thing on the DevHub.

if path.Status == Enum.PathStatus.Success then

Maybe try seeing what path.Status outputs in your code?

It is definitely a problem with the origin because when I give it an exact Vector3 value and not the randomNumber it works but not with the random number

EDIT

I’m giving it a vector3 now like this:

path:ComputeAsync(root.Position, Vector3.new(-390.3, 121.2, 189.8))

and its not working now
But when I give it a position of a part already in the game, it works.

Just found this and it fixed my problem:

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