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!