Only Finds 1/10 NPC's

Hello. I’m working on a NPC system where NPC can walk across the atmosphere without affecting performance! Haven’t really gotten to that point yet but that’s besides the point.

I’m working on a NPC system. But I’ve ran into a problem where it only identifies NPC #10 instead of all NPC’s 10/10. I’m confused why it’s doing this or how to fix it. It probably is a simple fix but I’m not sure what to do. Please assist me with a solution if you can. Thank you.

Code:

-- Services --
local Players = game:GetService('Players')
local CollectionService = game:GetService('CollectionService')
local PhysicsService = game:GetService('PhysicsService')
-- Variables --
local NPCFolder = workspace:FindFirstChild('NPCs')
local PlrGroup = 'PlrGroup'
local NPCGroup = 'NPCGroup'
local Increments = 75
local CurrentPart = nil
----

-- Collision Configuration --
PhysicsService:RegisterCollisionGroup(PlrGroup)
PhysicsService:RegisterCollisionGroup(NPCGroup)
----

-- Collision Setting / For Statement --
for _,Folder in ipairs(NPCFolder:GetChildren()) do
	-- Check --
	if Folder:IsA('Folder') then
		for _,Model in ipairs(Folder:GetChildren()) do
			if Model:IsA('Model') then
				print(Model)
				-- Check --
				for _,Asset in ipairs(Model:GetChildren()) do
					if Asset:IsA('BasePart') then
						-- Set Groups --
						Asset.CollisionGroup = NPCGroup
						Asset.Parent.PrimaryPart:SetNetworkOwner(nil)
					elseif Asset:IsA('Humanoid') then
						local Humanoid = Asset
						local Torso = Model['Torso']
						-- Disabled States --
						Humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing,false)
						Humanoid:SetStateEnabled(Enum.HumanoidStateType.Flying,false)
						Humanoid:SetStateEnabled(Enum.HumanoidStateType.StrafingNoPhysics,false)
						-- While Statement --
						while true do
							task.wait(5)
							Humanoid:MoveTo(Torso.Position + Vector3.new(math.random(-Increments,Increments),0,math.random(-Increments,Increments)),CurrentPart)
						end
					end
				end
			end
		end
	end
end
----

-- On Added --
Players.PlayerAdded:Connect(function(Plr)
	Plr.CharacterAppearanceLoaded:Connect(function(Character)
		-- For Statement --
		for _,Asset in ipairs(Character:GetChildren()) do
			if Asset:IsA('BasePart') then
				Asset.CollisionGroup = PlrGroup
			end
		end
	end)
end)
----

-- Init --
PhysicsService:CollisionGroupSetCollidable(PlrGroup,NPCGroup,false)
PhysicsService:CollisionGroupSetCollidable(NPCGroup,NPCGroup,false)
----

You have the while true do loop in the main for loop so it will not process the rest.
Use task.spawn():

task.spawn(function()
   while true do
      task.wait(5)
      Humanoid:MoveTo(Torso.Position + Vector3.new(math.random(-Increments,Increments),0,math.random(-Increments,Increments)),CurrentPart)
   end
end)

Also you should do more functions imo

1 Like

Also. How could I optimize this code so it doesn’t create any performance issues?

Not much, for the while loop it is in task.spawn() which is executed separately so it is automatically distributed across the cpu threads

Hold on. So I was never experiences in this but. How would I add a number to how many NPC’s can be added into the script. I’ve never really done something like that before

If you have some amount of different NPCs in the folder you can just limit it like that:

local LIMIT = 10 -- for example

for index, Folder in ipairs(NPCFolder:GetChildren()) do
-- Ik theres more but lets say Folder is a NPC
-- when you use ipairs the first value `index` is a number, it goes from one +1 for each element in folder here
-- so lets check if the index isn't higher than the limit
   if index > LIMIT then
      break
   end
-- break breaks the loop

If you have one NPC:

local AMOUNT = 10

for i = 1, AMOUNT do
-- code

If you have lets say 3 variants of NPCs:

local AMOUNT = 10

local NPCs = NPCFolder:GetChildren()

for i = 1, AMOUNT do
   local CurrentNPC = NPCs[math.random(1, #NPCs)]
   -- code

If you want to collect all the NPCs you summoned just add them to the table on creation:

local NPCs = {}

for index, Folder in ipairs(NPCFolder:GetChildren()) do
   -- Ik theres more but lets say Folder is a NPC
   table.insert(NPCs, Folder)
   -- code
-- to remove the npc:
table.remove(NPCs, table.find(NPCs, Folder)) -- it will sort the table so not nil holes
if #NPCs >= LIMIT then
   break
end

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