Spawn more objects in time

Hello !
I wanted to make a system where, as the time goes further, more objects would spawn. Pretty simple first look, but i can’t find a proper way to do it.
I have this spawn script :

function SpawnPart()
  local part = Instance.new('Part');
  part.Parent = workspace;
  local x, y, z = math.random(0, 5), math.random(0, 5), math.random(0, 5);
  part.Size = Vector3.new(x, y, z);
  print("Part spawned !");
end

while wait() do
  SpawnPart();
end

Thanks for futur answers !

I think this should work but I am not sure so try it at your own risk.

function SpawnPart()
local part = Instance.new(‘Part’);
part.Parent = workspace;
local x, y, z = math.random(0, 5), math.random(0, 5), math.random(0, 5);
part.Size = Vector3.new(x, y, z);
print(“Part spawned !”);
end

while true do
SpawnPart();
wait()
break
end

Inside the parenthesis of wait before break put the amount of seconds you want the code to run for. If you do not enter the time it will run till infinity and crash your computer.

Btw I am new to programming so I might be wrong.

i actually want to run it forever, and have an increment of part, this is linear, and i want to spawn more parts as the game goes further

If I am not not you want it to run forever, then I think the best option is to use one of these loops.

Just create a variable of the amount of objects to spawn

local partsToSpawn = 5

local function spawnParts()
    for i = 1, partsToSpawn do
        local part = Instance.new("Part", workspace)
        local local x, y, z = math.random(0, 5), math.random(0, 5), math.random(0, 5)
        part.Size = Vector3.new(x, y, z)
    end
end

while wait(1) do
    spawnParts()
    partsToSpawn = partsToSpawn * 2
end

Also you don’t need ; after a line

1 Like
local SpawnableParts	=	1

while wait(1) do
	for _ = 1, SpawnableParts do
		local Part	=	Instance.new("Part")
		Part.Size	=	Vector3.new(
			math.random(0, 5),
			math.random(0, 5),
			math.random(0, 5)
		)
		Part.Parent	=	workspace
	end
	SpawnableParts	+=	1
end
1 Like

Thank you !
I used to put this :

local startTime = os.time();
function Multiply()
 return (os.time() - startTime);
end
function Spawn()
  local part = Instance.new('Part');
  part.Parent = workspace;
  local x, y, z = math.random(0, 5), math.random(0, 5), math.random(0, 5);
  part.Size = Vector3.new(x, y, z);
  print("Part spawned !");
end
while true do
 Spawn()
 wait(1/Multiply)
end

But it was unefficient, this way is better!