The cycle does not work

I don’t understand why the loop doesn’t go beyond the line if npc:IsA("Model") and npc.Name~="Dummy" then--true.

--Create a NPC iteration loop
for _,npc in pairs(replicatedStorage:GetChildren()) do
	if npc:IsA("Model") and npc.Name~="Dummy" then--true
		for _,part in pairs(workspace.NPCPartPosition:GetChildren()) do
			if part:IsA("BasePart") and part.BrickColor==BrickColor.new("Really black") and part.Name~="SpawnPart" then
				walkingCycle(npc,part.Position)--Pass the position to the main function
				print("Success")
			end
		end
	end
end
3 Likes

What exactly are you trying to accomplish here? A little insight must be provided in order for us to understand what you are trying to do.

Are you sure it doesn’t get past that? Make a print and see if it does. Also could you show us your replicated storage directory?

I don’t want to spend a lot of time on this and I don’t see any point in hiding the script, so here is the full source code:

local player=game:GetService("Players")
local pathfinding=game:GetService("PathfindingService")

local replicatedStorage=game:GetService("ReplicatedStorage")
local parametersModule=require(replicatedStorage:WaitForChild("RandomizeNPCParameters"))

local function findPath(npc,target)
	local path=pathfinding:CreatePath()
	path:ComputeAsync(npc.PrimaryPart.Position,target)
	return path
end

local function visualizePath(path)
	local waypoints=path:GetWaypoints()
	local folder=Instance.new("Folder",workspace)
	folder.Name="PathCollections"
	
	for _,waypoint in pairs(waypoints) do
		local part=Instance.new("Part")
		part.Shape=Enum.PartType.Ball
		part.Material=Enum.Material.Neon
		part.BrickColor=BrickColor.Yellow()
		part.Size=Vector3.new(1,1,1)
		part.Position=waypoint.Position
		part.Anchored=true
		part.CanCollide=false
		part.Parent=folder
		task.wait(1)
	end
end

local function walkingCycle(npc,targetPosition)
	--if npc then
		parametersModule.spawnNPC(npc,2,4,workspace:WaitForChild("NPCPartPosition"):WaitForChild("SpawnPart"),10,true)
		if npc:FindFirstChild("Humanoid") and npc:FindFirstChild("HumanoidRootPart") then
			--Создаём путь
			local path=findPath(npc,targetPosition)
			--Проверяем, что путь найден
			if path.Status==Enum.PathStatus.Success then
				visualizePath(path)
			end
		else
			warn("NPC is nil in walkingCycle function.")
		end
	--end
end

for _,npc in pairs(replicatedStorage:GetChildren()) do
	if npc:IsA("Model") and npc.Name~="Dummy" then--true
		for _,part in pairs(workspace.NPCPartPosition:GetChildren()) do
			if part:IsA("BasePart") and part.BrickColor==BrickColor.new("Really black") and part.Name~="SpawnPart" then
				walkingCycle(npc,part.Position)
				print("Success")
			end
		end
	end
end

player.LocalPlayer.CharacterAdded:Connect(function(character)
	walkingCycle()
	print(character.Name.." has been connected.")
end)

player.LocalPlayer.CharacterRemoving:Connect(function(character)
	print(character.Name.." has been disconnected.")
end)

I’m trying to spawn NPCs on a certain platform with a certain amount, after which they should walk on pre-set parts infinitely(in progress).

изображение

Are you sure that you are supposed to run this AI script on the client?
Also the NPCs are under ReplicatedStorage, parent them to the Workspace instead.

Yes, the script must be on the client, and changing the parent of objects does not make sense. The problem is in the cycle, which for some reason does not work as it should.

Try to print something after each iteration of the loop, use ipairs.

(How else are you going to see the AI actually move?)

I use a module script that clones NPCs into a special folder in the workspace. I’ll try using ipairs.

If that’s what you meant.

  time:time:31.804  Invalid NPC or name is Dummy: RandomizeNPCParameters  -  Client - AdvancedSmartNPCSystem:70
  time:time:31.804  Checking NPC: Rig4  -  Client - AdvancedSmartNPCSystem:60
  time:time:31.804  Checking NPC: Rig1  -  Client - AdvancedSmartNPCSystem:60
  time:time:31.804  Invalid NPC or name is Dummy: Dummy  -  Client - AdvancedSmartNPCSystem:70
  time:time:31.804  Checking NPC: Rig2  -  Client - AdvancedSmartNPCSystem:60
  time:time:31.804  Checking NPC: Rig3  -  Client - AdvancedSmartNPCSystem:60

A good thing for troubleshooting is to print your variables before each if check to see what is or isn’t happening. Then you can troubleshoot why by going to the section of code that sets the variables.
Try this kind of thing:

for _,npc in pairs(replicatedStorage:GetChildren()) do
    print("npc.Name = ", npc.Name) 
    -- If it prints "nil" or something unexpected then look at how your code is written.
	if npc:IsA("Model") and npc.Name~="Dummy" then--true
1 Like

This is a good tip, but I already fixed the problem. But a new one appeared, for some reason the NPCs do not go the right way, although it is properly rendered, maybe the problem is that I use a local script, but I’m not sure.

So in the code where you set the NPC’s goal print the goal Position. It might give you a clue where your issue is.

For some reason, the loop once again does not go beyond the line for _,part in pairs(workspace.NPCPartPosition:GetChildren()):

for _,npc in ipairs(npcs) do
	for _,part in pairs(workspace.NPCPartPosition:GetChildren()) do--Stop line
		if part:IsA("BasePart") and part.BrickColor==BrickColor.new("Really black") and part.Name~="SpawnPart" then
			walkingCycle(npc,part.Position)
		end
	end
end
1 Like

Try printing

(workspace.NPCPartPosition:GetChildren())

to see what that actually gives you.

In general, as I understand it, pathfindingService is not replicated across the server-client boundary, so I will have to remake the script on the server side and then it should work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.