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