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