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

Pathfinding Zombie not Updating the path?

Asked by 4 years ago

I have a zombie that uses Pathfinding. It's follows and calculates the path to the player. I have a module script running these functions and a server script calling those functions.

The problem is it focuses on just moving on to the end of the path and not follow the path wherever the player is. Basically the zombie only follows the path of the player rather than the player itself.

I have tried to call the function that creates the path to the player every time the zombie moves but it's not working. I'm not sure what to do since I think I'm yielding the script too long.

I tried to fix the problem in another way by removing Humanoid.MoveToFinished:Wait() but that just doesn't move the zombie at all. There are no errors at all, but it's just not doing what I want it to do.

if you would like here is the functions from the module script running these functions:

Here is the function that is computing the path:

function ZombieClass:GetPath() -- in progress.
    local Target = ZombieClass:GetNearestPlayer().Character.HumanoidRootPart
    Path = PathFindingService:CreatePath() -- path is a global variable that changes
    Path:ComputeAsync(ZombieHumanoidRootPart.Position, Target.Position)
    ZombieClass:MoveZombieToWaypoints()
    return Target
end

Here is the function that is getting the nearest player:

function ZombieClass:GetNearestPlayer()
    local Players = game.Players:GetPlayers()
    local LowestDistance = (ZombieHumanoidRootPart.Position - Players[1].Character.HumanoidRootPart.Position).Magnitude
    local LowestPlayer = Players[1]

    for i,v in pairs(Players) do
        local ValueDistance = (ZombieHumanoidRootPart.Position - v.Character.HumanoidRootPart.Position).Magnitude
        if ValueDistance < LowestDistance then
            LowestDistance = ValueDistance
            LowestPlayer = v
        end
    end

    return LowestPlayer
end

Here is the function that is moving the zombie to the waypoints:

function ZombieClass:MoveZombieToWaypoints()
    local Waypoints = Path:GetWaypoints()
    for i,v in pairs(Waypoints) do
        ZombieHumanoid:MoveTo(v.Position)

        ZombieHumanoid.MoveToFinished:Wait()
    end
end

Here is the script for calling all of these functions:

local ZombieClass = require(script.Parent.ZombieModuleScript)

while true do
    wait(.05)
    ZombieClass:GetPath()
end

This seems like a lengthy question so feel free to ask questions about it

Answer this question