How can i make random npc spawn using invisible parts?

I am making a game and i have npc’s there. I want this npcs spawn at the invisible parts i will create but for example i want to have like 10 spawn points (invisible parts) and i want to spawn only 5 npc in random parts and then after i will defeat 1 npc new npc will spawn in the random part that is ready.

This is script i currently use but this script fills the all spawn points and do not leave some for next spawns. After i defeat npc he spawn at the previsious place (the same place). How to make it leave some places and then spawn in random?

local RS = game:GetService("ReplicatedStorage")
local spawnloc = workspace:WaitForChild("SpawnLoc")

while true do
for _, location in pairs(spawnloc:GetChildren()) do
	if location.ClassName == "Part" and location:WaitForChild("Ready").Value == true then
		location:WaitForChild("Ready").Value = false
		local npc = RS:WaitForChild("NPCs"):FindFirstChild(location.Name):Clone()
		npc.HumanoidRootPart.CFrame =
		npc.Parent = workspace["Zone 1"]

Well, have you thought about breaking the loop through the table when the amount of NPCs spawned reaches a certain number? That could be the reason why it spawns all the NPCs.

1 Like

Haven’t tryed this before lol. I just improved my script and yeah this work! thank you :slight_smile:

1 Like