I need help with making an NPC spawner. I want a limit for each NPC to spawn and spawn the NPC on the right spawn.
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local npcs = workspace:WaitForChild("Npcs")
local npcSpawns = workspace:WaitForChild("NpcSpawns")
local npcsFolder = serverStorage:WaitForChild("Npcs")
local npcLimit = 100
local dummyLimit = 75
local copsLimit = 25
local function randomSpawn()
local children = npcSpawns:GetChildren()
local random = children[math.random(1, #children)]
if random then
return random
end
end
local function Generate()
local children = npcsFolder:GetChildren()
local randomNpc = children[math.random(1, #children)]
local randomSpawner = randomSpawn()
if randomNpc then
local new = randomNpc:Clone()
new:PivotTo(CFrame.new(randomSpawner.Position))
new.Parent = npcs
end
end
local function spawner()
local child = #npcs:GetChildren()
if child < npcLimit then
Generate()
end
end
while task.wait(0.1) do
spawner()
end
I want them to spawn on the right spawn depending on the name.
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local Modules = replicatedStorage:WaitForChild("Modules")
local Remotes = replicatedStorage:WaitForChild("Remotes")
local npcs = workspace:WaitForChild("Npcs")
local npcSpawns = workspace:WaitForChild("NpcSpawns")
local npcsFolder = serverStorage:WaitForChild("Npcs")
local npcLimit = 100
local dummyLimit = 75
local copsLimit = 25
local dummies = 0
local cops = 0
local function randomSpawn()
local children = npcSpawns:GetChildren()
local random = children[math.random(1, #children)]
if random then
return random
end
end
local function Generate()
local children = npcsFolder:GetChildren()
local randomNpc = children[math.random(1, #children)]
local randomSpawner = randomSpawn()
if randomNpc then
local new = randomNpc:Clone()
new:PivotTo(CFrame.new(randomSpawner.Position))
new.Parent = npcs
end
end
local function spawner()
local child = #npcs:GetChildren()
if child < npcLimit then
Generate()
end
end
npcs.ChildAdded:Connect(function(child)
if child.Name == "Dummy" then
dummies += 1
elseif child.Name == "Cop" then
cops += 1
end
end)
npcs.ChildRemoved:Connect(function(child)
if child.Name == "Dummy" then
dummies -= 1
elseif child.Name == "Cop" then
cops -= 1
end
end)
while task.wait(0.1) do
spawner()
end
--//Services//--
local ServStorageService = game:GetService("ServerStorage")
local WorkspaceService = game:GetService("Workspace")
--//Variables//--
local Npcs = WorkspaceService:WaitForChild("Npcs" ,300)
local NpcSpawns = WorkspaceService:WaitForChild("NpcSpawns" ,300)
local NpcsFolder = ServStorageService:WaitForChild("Npcs" ,300)
local NpcTable = {
Total = {0, 100};
Dummy = {0, 75};
Cops = {0, 25}
}
--//Functions//--
local function NpcRemoved(Child)
NpcTable.Total[1] -= 1
for Key, Values in pairs(NpcTable)do
if Key == Child.Name then
Key[1] -= 1
end
end
end
local function Spawning(SelectedNpc, SelectedSpawn)
local Clone = NpcsFolder:FindFirstChild(SelectedNpc):Clone()
Clone:PivotTo(CFrame.new(SelectedSpawn.Position))
Clone.Parent = Npcs
end
local function Randomise()
local NpcState = false
local SpawnState = false
local RandomTableNpc = {}
local Spawners = NpcSpawns:GetChildren()
local SelectedNpc = nil
local SelectedSpawn = nil
for Index in pairs(NpcTable) do
if not (Index == NpcTable.Total) then
table.insert(RandomTableNpc, Index)
end
end
repeat
local RandomNpc = RandomTableNpc[math.random(1, #RandomTableNpc)]
if NpcTable[RandomNpc][1] < NpcTable[RandomNpc][2]then
NpcState = true
NpcTable[RandomNpc][1] += 1
SelectedNpc = RandomNpc
end
task.wait()
until
NpcState == true
repeat
local RandomSpawn = Spawners[math.random(1, #Spawners)]
if RandomSpawn.Name == tostring(SelectedNpc)then
SpawnState = true
SelectedSpawn = RandomSpawn
end
until
SpawnState == true
Spawning(SelectedNpc, SelectedSpawn)
end
--//Connections//--
if Npcs and NpcSpawns and NpcsFolder then
Npcs.ChildRemoved:Connect(function(Child)
NpcRemoved(Child)
end)
coroutine.resume(coroutine.create(function()
while NpcTable.Total[1] < NpcTable.Total[2]do
task.wait(0.1)
NpcTable.Total[1] += 1
Randomise()
end
end))
end
local serverStorage = game:GetService("ServerStorage")
local replicatedStorage = game:GetService("ReplicatedStorage")
local Modules = replicatedStorage:WaitForChild("Modules")
local Remotes = replicatedStorage:WaitForChild("Remotes")
local npcs = workspace:WaitForChild("Npcs")
local npcSpawns = workspace:WaitForChild("NpcSpawns")
local npcsFolder = serverStorage:WaitForChild("Npcs")
local limit = 100
local NpcTable = {
Dummy = {0, 75},
Cop = {0, 25}
}
local function checkLimit()
local children = npcs:GetChildren()
if #children >= limit then
return true
else
return false
end
end
local function NpcRemoved(Child)
for Key, Values in pairs(NpcTable)do
if Key == Child.Name then
Key[1] -= 1
end
end
end
local function Spawning(SelectedNpc, SelectedSpawn)
local newNpc = SelectedNpc:Clone()
newNpc:PivotTo(CFrame.new(SelectedSpawn.Position + Vector3.new(0,3,0)))
newNpc.Parent = npcs
end
local function randomNpc()
local children = npcsFolder:GetChildren()
local randomNpc = children[math.random(1, #children)]
if randomNpc then
return randomNpc
end
end
local function Randomise()
local NpcState = false
local SpawnState = false
local RandomTableNpc = {}
local Spawners = npcSpawns:GetChildren()
local SelectedNpc = nil
local SelectedSpawn = nil
for Index in pairs(NpcTable) do
if checkLimit() == false then
table.insert(RandomTableNpc, Index)
end
end
repeat
local RandomNpc = RandomTableNpc[math.random(1, #RandomTableNpc)]
if NpcTable[RandomNpc][1] < NpcTable[RandomNpc][2] then
NpcState = true
NpcTable[RandomNpc][1] += 1
SelectedNpc = randomNpc()
end
task.wait(0.1)
until NpcState == true
repeat
local RandomSpawn = Spawners[math.random(1, #Spawners)]
if RandomSpawn.Name == SelectedNpc.Name then
SpawnState = true
SelectedSpawn = RandomSpawn
end
task.wait(0.1)
until SpawnState == true
Spawning(SelectedNpc, SelectedSpawn)
end
npcs.ChildRemoved:Connect(function(Child)
NpcRemoved(Child)
end)
coroutine.wrap(function()
while task.wait(0.1) do
if checkLimit() == false then
Randomise()
end
end
end)()