Npc spawner that caps to amount of players

i tried to make a script that would cap the amount of npcs spawned to the amount of players

local NPC = game.ReplicatedStorage.Gunman
local spawner = script.Parent
local player_count = #game:GetService(“Players”):GetPlayers()
local npc_count = #workspace:FindFirstChild(“gunman”)

while true do
local Clone = NPC:Clone()
Clone.Torso.CFrame = spawner.CFrame
Clone.Parent = workspace
if player_count == 1 then
if npc_count == 5 then
break
end
end
end

not really the best but i tried

This might be what you are looking for.

local Players = game:GetService("Players")

local NPC_Count = 0

local function createNPC() -- I recommend making an NPC class, and making this a method
    NPC_Count += 1
    local Clone = NPC:Clone()
    Clone.Torso.CFrame = spawner.CFrame
    Clone.Parent = workspace
end

local function update()
    local playerCount = #Players:GetPlayers()
    while NPC_Count < playerCount do
        createNPC()
        task.wait()
    end
end

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