Heya there.
I’m rewriting an OOP NPC System. There’s however an issue with the system. The issue being that whenever a new NPC spawns in when one NPC is in the workspace, the first NPC immediately stops all movement.
--[[SERVICES]]--
local Players = game:GetService("Players")
local PathfindingService = game:GetService("PathfindingService")
--[[NPC MODEL FOLDERS]]--
local NPCFolder = game:GetService("ServerScriptService").NPCS
local NoobsFolder = NPCFolder.Noobs
--[[MODULE]]--
local BaseNPCModule = {}
BaseNPCModule.__index = BaseNPCModule
function BaseNPCModule:CreateNPC(UnitName, UnitTeam, Weapon, Special)
--//Checking to see if the unit exists.
for _, FindUnit in pairs(NoobsFolder:GetChildren()) do
if FindUnit:IsA("Model") and FindUnit.Name == UnitName then
--//Creating the unit.
self.Unit = FindUnit:Clone()
self.Humanoid = self.Unit:FindFirstChildWhichIsA("Humanoid")
self.Unit.Parent = game.Workspace
self.UnitTeam = game:GetService("CollectionService"):AddTag(self.Unit, UnitTeam)
self.Is_Special = Special
--//Giving it functionality
if not self.Is_Special then
--//Assuming it's just a basic NPC (E.g. Noob/Zombie Sworder)
--//Otherwise, the NPC is going to inheritate only the FindNearestTargets and Pathfind functions.
task.spawn(function()
while task.wait(0.05) do
task.spawn(function()
local Target = BaseNPCModule:FindNearestTargets()
if Target then
BaseNPCModule:Pathfind(Target)
end
end)
end
end)
end
end
end
end
function BaseNPCModule:FindNearestTargets()
local Targets = {}
local Nearest = math.huge
local Target = nil
for _, PossibleTargets in ipairs(workspace:GetChildren()) do
if PossibleTargets:IsA("Model") and PossibleTargets:FindFirstChildWhichIsA("Humanoid") then
local Humanoid = PossibleTargets:FindFirstChildWhichIsA("Humanoid")
if Humanoid.Health <= 1 then
--//They're dead.
return
end
--//If they aren't dead, check the distance.
local Distance = (PossibleTargets.PrimaryPart.Position - self.Unit.PrimaryPart.Position).Magnitude
if Distance <= Nearest then
if PossibleTargets:HasTag("Zombies") and self.Unit:HasTag("Noobs") then
table.insert(Targets,{
Magnitude = Nearest,
FoundTarget = PossibleTargets
})
elseif PossibleTargets:HasTag("Noobs") and self.Unit:HasTag("Zombies") then
table.insert(Targets,{
Magnitude = Nearest,
FoundTarget = PossibleTargets
})
end
end
end
end
for _, Entry in pairs(Targets) do
local magnitude = Entry.Magnitude
local target = Entry.FoundTarget
if magnitude <= Nearest then
Nearest = magnitude
Target = target
end
end
return Target
end
function BaseNPCModule:Pathfind(Target)
local NewPath = PathfindingService:CreatePath({
AgentRadius = 3,
AgentHeight = 5,
AgentCanJump = true,
})
NewPath:ComputeAsync(self.Unit.PrimaryPart.Position, Target.PrimaryPart.Position)
if NewPath.Status == Enum.PathStatus.Success then
local Waypoints = NewPath:GetWaypoints()
for i, Waypoint in ipairs(Waypoints) do
if i <= 2 then continue end
if Waypoints[4] then
self.Humanoid:MoveTo(Waypoints[4].Position)
local TimeOut = self.Humanoid.MoveToFinished:Wait()
if not TimeOut then
warn("NPC got stuck... Recalculating!")
BaseNPCModule:Pathfind(Target)
end
end
end
end
end
return BaseNPCModule