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 6 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

01local pathFindingService = game:GetService("PathfindingService");
02local path = pathFindingService:CreatePath();
03local NPC = script.Parent;
04local NPC_Humanoid = NPC.Humanoid;
05 
06local waypoints;
07local currentWaypointIndex;
08 
09local function findNearestPlayer(position)
10    local lowest = math.huge;
11    local nearestPlayer;
12 
13    for _, v in pairs(game.Players:GetPlayers()) do
14        if v then
15            if v.Character then
View all 73 lines...

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 — 6y
0
Shut up incapaz ScriptingEngine 5 — 6y
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 — 6y
0
Nope, 16 and yes ScriptingEngine 5 — 6y
0
@ScriptingEngine greatneil80 2647 — 6y

2 answers

Log in to vote
0
Answered by 6 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.

01path.Blocked:Connect(onPathBlocked)
02 
03NPC_Humanoid.MoveToFinished:Connect(onWaypointReached)
04 
05while true do
06    wait(5);
07    local nearestPlayer = findNearestPlayer(NPC.HumanoidRootPart.Position);
08 
09    if nearestPlayer then
10        followPath(nearestPlayer);
11    end
12end
Ad
Log in to vote
0
Answered by
arshad145 392 Moderation Voter
6 years ago

Hello,

01local pathFindingService = game:GetService("PathfindingService");
02local path = pathFindingService:CreatePath();
03local NPC = script.Parent;
04local NPC_Humanoid = NPC.Humanoid;
05 
06local waypoints;
07local currentWaypointIndex = 1;
08 
09local function findNearestPlayer(position)
10    local lowest = math.huge;
11    local nearestPlayer;
12 
13    for _, v in pairs(game.Players:GetPlayers()) do
14        if v then
15            if v.Character then
View all 70 lines...

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