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

Trying to learn about the Pathfinding Service but my MoveTo is not working?

Asked by 5 years ago

WARNING: NOT THE PRETTIEST CODE IM JUST TRYING TO LEARN HOW TO USE THE PathfindingService

So the problem is with the MoveTo on the followPath function on line 37

local pathFindingService = game:GetService("PathfindingService");
local path = pathFindingService:CreatePath();
local NPC = script.Parent;
local NPC_Humanoid = NPC.Humanoid;

local waypoints;
local currentWaypointIndex;

local function findNearestPlayer(position)
    local lowest = math.huge;
    local nearestPlayer;

    for _, v in pairs(game.Players:GetPlayers()) do
        if v then
            if v.Character then
                local distance = v:DistanceFromCharacter(position);
                if distance <= lowest then
                    lowest = distance;
                    nearestPlayer = v;
                    print(nearestPlayer.Name.." is closest player at a distance of :".. distance );
                    return nearestPlayer;
                end
            end
        end
    end
end

local function followPath(player)
    path:ComputeAsync(NPC.HumanoidRootPart.Position,    player.Character.HumanoidRootPart.Position);

    waypoints = {};
    if path.Status == Enum.PathStatus.Success then
        waypoints = path:GetWaypoints();
        print("lets see if i did a thing");
        currentWaypointIndex = 1;

        NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position);
        print("now it should be moving");
    else
        NPC_Humanoid:MoveTo(NPC.HumanoidRootPart.Position);
    end

end

local function onWaypointReached(reached)
    if reached and currentWaypointIndex < #waypoints then
        currentWaypointIndex = currentWaypointIndex + 1;
        NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position);
    end
end

local function onPathBlocked(blockedWaypoint)

    if blockedWaypoint > currentWaypointIndex then
        local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position);
        followPath(nearestPlayer);
    end
end


while true do
    wait(15);
    local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position);

    if nearestPlayer then
        followPath(nearestPlayer);
    end
end


path.Blocked:Connect(onPathBlocked)

NPC_Humanoid.MoveToFinished:Connect(onWaypointReached)

It does change the WalkToPoint but the NPC (Clone of my character from in game) does not actually walk to that point I can't figure out why

Here is the WalkToPoint before MoveTo was called

Here it is after the MoveTo

and here is a gif showing the uneventful MoveTo (output should say "now it should be moving" after MoveTo was called)

also sorry for hurting your eyes with this ugly code I just want to figure this out I have been stuck for almost a day now .-. It worked like twice then it just stopped working.

0
alvinbloxx taught me put a wait at the top of my script so try that User#24403 69 — 5y
0
Shut up incapaz ScriptingEngine 5 — 5y
0
Are any of the parts in the character anchored? The humanoid's walkspeed is not 0 and PlatformStand is off, right? User#20279 0 — 5y
0
Nope, 16 and yes ScriptingEngine 5 — 5y
0
@ScriptingEngine greatneil80 2647 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

It's because you're connecting the events after the while loop. As the while loop doesn't let anything else after it run, the events will never connect.

Move the connects to before the while loop, and it should be fine.

path.Blocked:Connect(onPathBlocked)

NPC_Humanoid.MoveToFinished:Connect(onWaypointReached)

while true do
    wait(5);
    local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position);

    if nearestPlayer then
        followPath(nearestPlayer);
    end
end
Ad
Log in to vote
0
Answered by
arshad145 392 Moderation Voter
5 years ago

Hello,

local pathFindingService = game:GetService("PathfindingService");
local path = pathFindingService:CreatePath();
local NPC = script.Parent;
local NPC_Humanoid = NPC.Humanoid;

local waypoints;
local currentWaypointIndex = 1;

local function findNearestPlayer(position)
    local lowest = math.huge;
    local nearestPlayer;

    for _, v in pairs(game.Players:GetPlayers()) do
        if v then
            if v.Character then
                local distance = v:DistanceFromCharacter(position);
                if distance <= lowest then
                    lowest = distance;
                    nearestPlayer = v;
                    print(nearestPlayer.Name.." is closest player at a distance of :".. distance );
                    return nearestPlayer;
                end
            end
        end
    end
end

local function followPath(player)
    path:ComputeAsync(NPC.HumanoidRootPart.Position,    player.Character.HumanoidRootPart.Position);

    waypoints = {};
    if path.Status == Enum.PathStatus.Success then
        waypoints = path:GetWaypoints();
        -- currentWaypointIndex = 1; -- You are declaring the index being 1.
        print(currentWaypointIndex)
        NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position);
    else
        NPC_Humanoid:MoveTo(NPC.HumanoidRootPart.Position);
    end

end

local function onWaypointReached(reached)
    if reached and currentWaypointIndex < #waypoints then
        currentWaypointIndex = currentWaypointIndex + 1;
    NPC_Humanoid:MoveTo(waypoints[currentWaypointIndex].Position);
    end
end

local function onPathBlocked(blockedWaypoint)

    if blockedWaypoint > currentWaypointIndex then
        local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position);
        followPath(nearestPlayer);
    end
end


while true do
    wait(15);
    local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position);
    if nearestPlayer then
        followPath(nearestPlayer);
    end
end


path.Blocked:Connect(onPathBlocked)

NPC_Humanoid.MoveToFinished:Connect(onWaypointReached)

So, having redacted the mistake, I'll proceed to explain.

In the while loop, you are checking for the nearestPlayer and "following" it with the function followPath, for now everything was good, but on Line 24, you were declaring the currentWaypointIndex to be 1, constantly as it is inside of a while loop.

To counter this, we make currentWaypointIndex a "global" in the scope of this script; Line 7.

This should fix your issue, any further questions DM me.

PS:Please accept this as an answer if this helped you.

Answer this question