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
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.
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
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
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