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

Why is my for in pairs() loop not running? (script includes pathfinding)

Asked by 4 years ago

Hello. I am trying to use PathfindingService to create a path for an NPC to walk when a player touches a part. At line 45 of the code (the one that says for _, in pairs()) , the for loop doesn't run. I think that's the case since the print() statement at line 47 wasn't printed.

the script goes like so: when the player steps on "activate", the function(OnPartTouched) will run.

i've included which statements have been printed. this script is in starter character scripts and its a local script.

any idea why for in pairs() loop isn't running?

any help is appreciated!

my code is pasted below.

01local PathFindingService = game:GetService("PathfindingService")
02local ReplicatedStorage = game:GetService("ReplicatedStorage")
03 
04local T1NPC1 = game.Workspace.T1NPC["1"].T1NPC1
05local NPCHumanoid = T1NPC1.Humanoid
06local Activate = game.Workspace.T1NPC["1"].Activate
07 
08 
09local path = PathFindingService:CreatePath()
10 
11local CanMakePath = true
12 
13local function OnPartTouched(hit)
14 
15    if CanMakePath == true then
View all 73 lines...
0
i forgot to mention that there is no error in the output Gmorcad12345 434 — 4y
0
do print(unpack(waypoints)) before the loop mixgingengerina10 223 — 4y
0
i've added it in, and it didn't print Gmorcad12345 434 — 4y
0
does that mean that the script/pathfindingservice couldn't get any waypoints? Gmorcad12345 434 — 4y

2 answers

Log in to vote
0
Answered by
Elyzzia 1294 Moderation Voter
4 years ago
Edited 4 years ago

if PathfindingService isn't able to find a valid path between the starting and ending point, or if there's something intersecting the starting point or ending point, it won't create any waypoints

it's probably caused by the ending point being 2.5 studs below the player ((which is literally right next to the ground))

also, there's no reason to create a new part just to get its position, you can just use the HumanoidRootPart's position directly

also also, even if PathfindingService was able to create the waypoints, unless the client has network ownership over the npc for some reason or the npc was created on the client, they wouldn't be able to move it, so you'd have to move this to the server anyway

0
there are no obstacles stopping the NPC and i've already edited to script so that its destination is the player's humanoidrootpart. however there are still no waypoints. could u help me? thx for your time Gmorcad12345 434 — 4y
0
also i can't move the script to the server as I only want the local player to be able to see the npc move Gmorcad12345 434 — 4y
0
my NPC is also parented to a folder in a workspace, just wondering if it affects the results Gmorcad12345 434 — 4y
0
or is the reason of the problem becoz of the local script Gmorcad12345 434 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

hey elyzzia, could you take a look at the comments below your answer? thx for your time and help

Answer this question