So I made a zombie that follows player with pathfinding script but the problem is that it waits for the zombie to move to the first spot instead of following the player
Video:
If I remove hum.MoveToFinished:Wait() then it the pathfind stops working
Script:
repeat
wait()
until script.Parent.Parent == workspace.Zombies
local Zombie = script.Parent
local PFS = game:GetService("PathfindingService")
local hum = script.Parent:WaitForChild("Zombie")
local RS = game:GetService("RunService")
local humrootpart = script.Parent:WaitForChild("HumanoidRootPart")
local path = PFS:CreatePath(
{
AgentRadius = 4,
AgentHeight = 6,
AgentCanJump = true,
Costs = {
Water = 20,
Climb = 1
}
}
)
local target = nil
local status = require(script.Parent:WaitForChild("Status"))
local attackdebounce = false
local jumpdelay = true
local hithumR = nil
local function findTarget()
local maxDistance = 999999
for i, player in pairs(game.Players:GetPlayers()) do
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")
local magnitude = (humrootpart.Position - char.HumanoidRootPart.Position).Magnitude
if magnitude <= maxDistance and char and humanoid.Health > 0 then
maxDistance = magnitude
target = char
end
end
return target
end
local function jump()
hum:ChangeState(Enum.HumanoidStateType.Jumping)
wait(status.JumpDelay)
jumpdelay = true
end
local function attack(target)
local hithum = target:FindFirstChildWhichIsA("Humanoid")
local hithumroot = target:FindFirstChild("HumanoidRootPart")
if hithumroot then
hithumR = hithumroot
if (humrootpart.Position - hithumroot.Position).Magnitude < 4.5 then
hum.WalkSpeed = 0
hum.AutoRotate = false
local hithum = target:FindFirstChildWhichIsA("Humanoid")
if hithum then
if hithum.Name ~= "Zombie" and attackdebounce == false and status.Dead ~= true and status.OnFire ~= true then
attackdebounce = true
hithum:TakeDamage(status.Damage)
local hitsnd = math.random(1,2)
if hitsnd == 1 then
script.Parent.HumanoidRootPart.PunchSnds.SoundId = "rbxassetid://12366696098"
elseif hitsnd == 2 then
script.Parent.HumanoidRootPart.PunchSnds.SoundId = "rbxassetid://12366696960"
end
script.Parent.HumanoidRootPart.PunchSnds:Play()
wait(status.AttackDelay)
attackdebounce = false
end
end
wait(1)
hum.AutoRotate = true
hum.WalkSpeed = status.WalkSpeed
end
end
end
while RS.Heartbeat:Wait() do
findTarget()
if target and status.Dead ~= true and status.Shoved == false then
hum.WalkSpeed = status.WalkSpeed
local temptarget = target:FindFirstChild("HumanoidRootPart")
if temptarget and status.Dead ~= true then
path:ComputeAsync(humrootpart.Position, temptarget.Position)
waypoints = path:GetWaypoints()
if path.Status == Enum.PathStatus.Success then
for i, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump and jumpdelay == true then
jumpdelay = false
jump()
end
if hithumR then
if (humrootpart.Position - hithumR.Position).Magnitude > 3 then
hum:MoveTo(waypoint.Position)
hum.MoveToFinished:Wait()
local ZombieFolder = workspace.Zombies
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {ZombieFolder, Zombie}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
local ray = workspace:Raycast(humrootpart.Position, hithumR.Position, raycastParams)
if ray and ray.Instance == hithumR then
print("see you")
print(ray.Instance)
break
else
if ray then
print(ray.Instance)
end
print("?")
end
else
hum:MoveTo(humrootpart.Position)
end
end
if status.OnFire ~= true then
attack(target)
end
end
end
end
elseif status.Dead ~= true then
if status.Shoved == true then
hum.WalkSpeed = 0
hum:MoveTo(humrootpart.Position)
else
hum.WalkSpeed = status.WalkSpeed
hum:MoveTo(humrootpart.Position)
end
end
end
Please help out, thank you!