I want to make each NPC spawn in a random place. Currently, when they spawn they all spawn at the same random place

I’m making a game where the players fight off a bunch of respawning NPC’s. I want the maximum amount of NPC’s at one time to be 50. I want them to each spawn at an unset separate place. When they spawn they all spawn together in a random place. Sorry if this doesn’t make much sense, this is my first post.

Here’s a picture of how they all spawn bunched up:
noobs

Here’s what I’d like it to look like:

The code:

local FIRSTNPCS = {}
local NPC = game.ReplicatedStorage.Enemies.Noob

--First Spawn
for i = 1, 50 do
	local FirstClonedNpc = NPC:Clone()
	table.insert(FIRSTNPCS, #FIRSTNPCS + 1, FirstClonedNpc)
end

for index, FirstClonedNPC in ipairs(FIRSTNPCS) do
	FirstClonedNPC.Parent = script.Parent.EnemyFolder
	FirstClonedNPC.PrimaryPart.CFrame = CFrame.new(math.random(script.Parent.A.CFrame.X, script.Parent.B.CFrame.X), 0, math.random(script.Parent.A.CFrame.Y, script.Parent.B.CFrame.Y))
end


--Repeat Spawns
while wait(30) do
	local NPCS = {}
	
	local children = script.Parent.EnemyFolder:GetChildren()
	local existing = #children
	
	amount = 50 - existing
	
	for i = 1, amount do
		local ClonedNpc = NPC:Clone()
		table.insert(NPCS, #NPCS + 1, ClonedNpc)
	end

	for index, ClonedNPC in ipairs(NPCS) do
		ClonedNPC.Parent = script.Parent.EnemyFolder
		ClonedNPC.PrimaryPart.CFrame = CFrame.new(math.random(script.Parent.A.CFrame.X, script.Parent.B.CFrame.X), 0, math.random(script.Parent.A.CFrame.Y, script.Parent.B.CFrame.Y))
	end
end
4 Likes

local randomX = math.random(1,1000)
local randomY = math.random(1,1000)
local randomZ = math.random(1,1000)

Npc.HumanoidRootPart.CFrame = cframe.New(randomX, randomY, randomZ)

should look like that maybe

1 Like

This wouldn’t really help because where they teleport already works, it’s just that I want each of them to teleport to a different place. With the current script it just teleports each of them to the same random place.

In addition to what @mlnitoon2 said,

there are several articles that may help you:

You can use this principle to implement that into your script.

local randomX = nil
local randomY = nil
local randomZ = nil

randomX = math.random(1,1000)
randomY = math.random(1,1000)
randomZ = math.random(1,1000)

Npc.HumanoidRootPart.CFrame = cframe.New(randomX, randomY, randomZ)

randomX = nil
randomY = nil
randomZ = nil

I appreciate your help but, The first one need set spawns to work, the second one also uses set spawns(the second one is still helpful), and the third one has to do with spawn locations. What I’m going for is kind of hard for me to articulate. I just need each NPC to spawn into a random place, Right now(in the bugged script), They spawn at a random point, but they all spawn at the SAME random point.

You can use math.random to spawn them in different areas, you can also check if there is a npc that has spawned on a specific area

I am currently using math.random as shown in the script. I’m going to try scripting to check if an npc has spawned in that area using a table with all of the CFrame values.

Best of luck mate! I now gotta go

local RandomSpawn = math.random(script.Parent.A.CFrame.X, script.Parent.B.CFrame.X), 0, math.random(script.Parent.A.CFrame.Y, script.Parent.B.CFrame.Y)
ClonedNPC.Parent = script.Parent.EnemyFolder
ClonedNPC.PrimaryPart.CFrame = CFrame.new(RandomSpawn)

I think that should work, also make sure that your loop actually runs few times using print.

oops, that actually wouldnt work. You have to use local math.random for every axis:

local RandomSpawnX = math.random(script.Parent.A.CFrame.X, script.Parent.B.CFrame.X)
local RandomSpawnY = math.random(script.Parent.A.CFrame.Y, script.Parent.B.CFrame.Y)
ClonedNPC.Parent = script.Parent.EnemyFolder
ClonedNPC.PrimaryPart.CFrame = CFrame.new(RandomSpawnX, 0, RandomSpawnY)

They still all spawn at the exact same random point.

Try using this script:

local chosenX
local chosenY
local RandomSpawnX 
local RandomSpawnY
for index, ClonedNPC in ipairs(NPCS) do
		RandomSpawnX = math.random(script.Parent.A.CFrame.X, script.Parent.B.CFrame.X)
if chosenX == RandomSpawnX then
 RandomSpawnX = math.random(script.Parent.A.CFrame.X, script.Parent.B.CFrame.X)
end
RandomSpawnY = math.random(script.Parent.A.CFrame.Y, script.Parent.B.CFrame.Y)
if chosenY == RandomSpawnY then
RandomSpawnY = math.random(script.Parent.A.CFrame.Y, script.Parent.B.CFrame.Y)
end
ClonedNPC.Parent = script.Parent.EnemyFolder
ClonedNPC.PrimaryPart.CFrame = CFrame.new(RandomSpawnX, 0, RandomSpawnY)
chosenX = RandomSpawnX
chosenY = RandomSpawnY 
	end

if it doesnt work i believe that there’s something wrong with your table. As i said before try printing index or anything to check if it runs few times or only one time.

1 Like

Workspace.EnemySpawner.Script:19: invalid argument #2 to ‘random’ (interval is empty) - Server - Script:19

Says there’s somethign wrong with this line:

RandomSpawnZ = math.random(script.Parent.A.CFrame.Z, script.Parent.B.CFrame.Z)

Also I checked by printing and it printed it 50 times.

invalid argument #2 to ‘random’ (interval is empty) propably means that your first number isnt bigger than second one, basically: script.Parent.A.CFrame.Z is less than script.Parent.B.CFrame.Z. You have to check and change those values.

1 Like

I’ve checked and A’s CFrame.Z value was more than B’s Cframe.Z value. It now works to an extent. They don’t ALL spawn in the same place but they all spawn in the same place in 4 or 5 groups now. I think it might be because the server needs some time to recover for each one so im gonna use task.wait() and see if that works.

I’ve added task.wait() and it works flawlessly. Thanks so much!

1 Like