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:
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
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.
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.
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.
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.
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.
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.