Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

local PathFindingService = game:GetService("PathfindingService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local T1NPC1 = game.Workspace.T1NPC["1"].T1NPC1
local NPCHumanoid = T1NPC1.Humanoid
local Activate = game.Workspace.T1NPC["1"].Activate


local path = PathFindingService:CreatePath()

local CanMakePath = true

local function OnPartTouched(hit)
	
	if CanMakePath == true then	
		
		CanMakePath = false

		print("Function fired")--printed
		local LocalPlayer = game.Players.LocalPlayer
		local LocalPlayerCharacter = LocalPlayer.Character
		local IsBattled = LocalPlayer.NPCsBattled.T1["1"].IsBattled
		
		if IsBattled.Value == false and LocalPlayerCharacter and LocalPlayerCharacter:FindFirstChildOfClass("Humanoid")then
			
			local PlayerHumanoid = LocalPlayerCharacter.Humanoid
			PlayerHumanoid.WalkSpeed = 0
			local CreatePart = Instance.new("Part")
			CreatePart.Position = LocalPlayerCharacter.HumanoidRootPart.Position - Vector3.new(0,2.5,0)
			CreatePart.Name = LocalPlayer.Name.."'sAlertPart" --this part has been created
			CreatePart.Transparency,CreatePart.Anchored,CreatePart.CanCollide = 1,true,false
			CreatePart.Parent = workspace
			print(LocalPlayer.Name.." has created a part due to encountering an NPC")--printed
			
			warn(tostring(LocalPlayer.Name.." is gonna create a part."))--warned/printed
			
			path:ComputeAsync(T1NPC1.HumanoidRootPart.Position,CreatePart.Position)
			
			print("the NPC gonna find a way to da player")--printed
			
			local waypoints = path:GetWaypoints()
			
			print("NPC got waypoints")--printed
			
			for _, waypoint in pairs(waypoints) do
				
				print("NPC will move") --this doesn't print, which means this for loop didn't fire at all
				local WP = Instance.new("Part")
				WP.Shape = "Ball"
				WP.Material = "Neon"
				WP.Size = Vector3.new(0.6, 0.6, 0.6)
				WP.Position = waypoint.Position
				WP.Anchored = true
				WP.CanCollide = false
				WP.Parent = game.Workspace
				
				NPCHumanoid:MoveTo(waypoint.Position)
				NPCHumanoid.MoveToFinished:Wait()
				
				
			end
			
		end
		
	end
	
	
	
end

Activate.Touched:Connect(OnPartTouched)
--this script is inside startercharacterscripts and its a Localscript
--there are no error messages

4 comments

0

i forgot to mention that there is no error in the output

0

do print(unpack(waypoints)) before the loop

0

i've added it in, and it didn't print

0

does that mean that the script/pathfindingservice couldn't get any waypoints?

2 answers

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

4 comments

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

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

0

my NPC is also parented to a folder in a workspace, just wondering if it affects the results

0

or is the reason of the problem becoz of the local script

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

This discussion is preserved as it was. New answers, comments, and votes are closed.