Zombie Pathfinding Not updating fast enough

I’m making a pathfinding script that is supposed to have a zombie chase a player down however it only goes after the player last position and I dont know how to get it to constantly update the position

Zombie Script

local Zombie = {}
Zombie.__index = Zombie

local ZombieTable = {}
local Path = require(game.ReplicatedStorage.ZombieSystem.Pathfinding)

function Zombie.new(Hp, Spd, Dmg, position)
 
	local NewZombie = setmetatable({}, Zombie)
	
	NewZombie.Model = game.ReplicatedStorage.ZombieModels.ZombieModel:Clone()
	NewZombie.Model.Parent = game.Workspace
	NewZombie.Model.HumanoidRootPart.Anchored = false
	NewZombie.Model.HumanoidRootPart.CanCollide = true
	NewZombie.Model:SetPrimaryPartCFrame(CFrame.new(position))
	
	
	NewZombie.health = Hp
	NewZombie.speed = Spd
	NewZombie.Model.Humanoid.WalkSpeed = NewZombie.speed
	NewZombie.damage = Dmg
	NewZombie.state = "Alive"
	
	table.insert(ZombieTable, NewZombie)
	
	
    
	task.spawn(function()
		task.wait(2)
	while true do
		NewZombie:test()
		task.wait(0.3)
	end	
	end)
		
		
	return NewZombie
end

--Pay attention to this function and the .new function for the problem
function Zombie:test()
	local Player = self:CheckForPlayers()
	local Path, Waypoints = Path:Create(self.Model.HumanoidRootPart.Position, Player)

	if Path and Waypoints then
	for _, Waypoints in ipairs(Waypoints) do
		self.Model.Humanoid:MoveTo(Waypoints.Position)
		self.Model.Humanoid.MoveToFinished:Wait()
	end
else
	warn("Path not found")
end
end

	


function Zombie:ChooseWaypoint()

	local PathfindingService = game:GetService("PathfindingService")

	local ReconPoints = game.Workspace.ReconPoints:GetChildren()
	local MaxDistance = 500
	local ReconTable = {}

	--Get all ReconPoints near it and choose one to randomly go to 
	for _, ReconPoint in pairs(ReconPoints) do
		local DistanceFromReconPoint = (ReconPoint.Position - self.Model.HumanoidRootPart.Position).Magnitude
		if DistanceFromReconPoint < MaxDistance then
			table.insert(ReconTable, ReconPoint)
		end
	end

	if #ReconTable == 0 then
		return
	end

	local ReconPointPickedNum = math.random(1, #ReconTable)
	local ReconPointPicked = ReconTable[ReconPointPickedNum]

	return ReconPointPicked.Position
end

function Zombie:CheckForPlayers()
	local Players = game.Players:GetPlayers()
	local nearestPlayer 
	local Maxdistance = 500

	for _, player in pairs(Players) do
		local PlayerDistance = (player.Character.HumanoidRootPart.Position - self.Model.HumanoidRootPart.Position).Magnitude
		if PlayerDistance < Maxdistance then
			nearestPlayer = player
			Maxdistance = PlayerDistance
		end
	end
	
	if nearestPlayer == nil then
		print("No players")
		return nil
	end
	
	local NewZombieTable = self:GetAllZombies()

	local RayDirection = (nearestPlayer.Character.Head.Position - self.Model.Head.Position).Unit * 500
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = NewZombieTable
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude

	local RaycastResult = workspace:Raycast(self.Model.Head.Position, RayDirection, raycastParams)

	if RaycastResult.Instance:IsDescendantOf(nearestPlayer.Character) or RaycastResult.Instance == nearestPlayer.Character then


		return nearestPlayer.Character.HumanoidRootPart.Position
		
	else
        print("No player found")
		return nil
	end
end

function Zombie:GetAllZombies()
	local NewZombieTable = {}
	for _, Zombie in ipairs(ZombieTable) do
		table.insert(NewZombieTable, Zombie.Model)
	end
	return NewZombieTable
end
			
return Zombie

Pathfinding Script:

local Path = {}

local PathfindingService = game:GetService("PathfindingService")

function Path:Create(Start, Goal)
local Path = PathfindingService:CreatePath()

local success, errormessage = pcall(function()
Path:ComputeAsync(Start, Goal)
end)

if success then
    local Waypoints = Path:GetWaypoints()	
    return Path, Waypoints
        else
	    warn(errormessage)
        end
end

return Path

Any help is much appreciated!

1 Like

It should be zombie:new, not zombie.new

nah its zombie.new It’s a contructer function

1 Like

That’s the constructer therefor it would be zombie.new not zomebie:new.

what do you mean by go after last position?

You will have to optimize pathfinding to suit your needs for zombies to update automatically.
You could achieve this by having navMesh and then if zombie is outside of desired navmesh, move him back into it by humanoid:MoveTo()

You could combine humanoid:MoveTo() with raycating and checking if something is inbetween zombie and player and if not, calculate pathfinding.

You also do not wanna do this logic for every zombie, so you might have to implement something beyond this.

The problem here is that Move:To() is too inaccurate and whenever I try implementing actual pathfinding the zombie lags alot and twiches. I’ll mark your comment as solution cause this topic is old.

Consider using :Move() instead of :MoveTo(). Instead of inputting a position, you’d input the direction from the zombie to the player’s character. This should solve the lagging and twitching.

Can’t The zombie needs to be able to pathfind around obstacles

I use :Move() and they all seem to find the correct path very well (Watch ai | Streamable). In your case I would comment out that MoveToFinished wield if you want to try this method.

I’m looking at the documentation and it seems that :Move() only makes a player or humanoid move in a certain direction without pathfinding is that video yours. And if so How did you use :Move() to do that

Use the direction of the character to the next waypoint (simply hum:Move(waypoint.Position - rootPart.Position)) as the parameter. I apologize for the late reply.

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