Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Pathfind only update once it's route is first complete?

Asked by
Kryddan 261 Moderation Voter
8 years ago

So I have this zombie that is supposed to path find to the closest player. It works perfectly without one thing, the zombie will only update the path when he reaches his target position. https://gyazo.com/bdd1ce536fbe8d157c816b32d1a5b212

--Zombie
local torso = script.Parent.Torso
local targ = nil
local db = false
local pathService = game:GetService("PathfindingService")
local zombieDamage = 50
local zombieWait = 1



function getClose()
    local ptorso = game.Players:GetChildren()
    local ptab = {}
    for i = 1, #ptorso do
        table.insert(ptab, i, (torso.Position-ptorso[i].Character.Torso.Position).magnitude)

    end
    table.sort(ptab)
    for i = 1, #ptorso do
        if (torso.Position-ptorso[i].Character.Torso.Position).magnitude == ptab[1] then
            targ = ptorso[i]            
        end
    end
end

function pathfind()
  local path = pathService:ComputeRawPathAsync(torso.Position, targ.Character.Torso.Position, 512)
  local points = path:GetPointCoordinates()

    for _,v in pairs(points) do
    local part = Instance.new("Part")
    part.Size = Vector3.new(1,1,1)
    part.Anchored = true
    part.CanCollide = false
    part.Position = v 
    part.Parent = workspace
    game:GetService("Debris"):AddItem(part,0.1) 

    wait()
end

  for _,point in pairs (points) do
    torso.Parent.Humanoid:MoveTo(point)
    while (torso.Position - point).magnitude > 4 do
        wait(0.1)
    end
  end
end


torso.Parent.Humanoid.Died:connect(function()
    wait(0.5)
    torso.Parent:Destroy()
end)



function attack()
    if db == false then
        db = true
        local human = targ.Character:FindFirstChild("Humanoid")
        if human ~= nil and human.Health > 0 then
        human:TakeDamage(zombieDamage)
        wait(zombieWait)
        db = false
      end
    end
end


while wait() do
    getClose()
    pathfind()  --here the problem is located I think? Do I have to use coroutines and if so, how?
    if (torso.Position-targ.Character.Torso.Position).magnitude < 3 then
        attack()
    end
end






Answer this question