Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to add a limit to a parts spawning script?

Hello! So basically what I said above. I'm making a game and I have this partial regen system that spawns parts. I was wondering how I could put a limit to how many parts there are!

Here is my code:

local TrashSpawnArea = workspace.TrashSpawnArea
local Trash = workspace.Trash
local r = 255

while wait(1) do
	local TrashClone = Trash:Clone()
	local x,z = TrashSpawnArea.Position.X, TrashSpawnArea.Position.Z
	x,z = math.random(x - r, x + r), math.random(z - r, z + r)
	local pos = Vector3.new(x, TrashSpawnArea.Position.Y, z)
	TrashClone.Parent = workspace
	TrashClone.Position = pos
end

r would equal to the radius (or the area) that the parts spawn in. It's set to 255 because thats the default baseplate size.

This one is confusing to me, any help is appreciated!

p.s my script is in ServerScriptService

p.s.s don't mind me calling it trash lol

1 answer

I have a solution (sorta). So you could put a value and add to it every time a clone spawns, and then have an if then for the amount of times the thing spawns.

local TrashSpawnArea = workspace.TrashSpawnArea
local Trash = workspace.Trash
local numb = 0
local r = 255

while wait(1) do
if numb < 3 then
    local TrashClone = Trash:Clone()
    TrashClone.Name = "TheName=/"
    local x,z = TrashSpawnArea.Position.X, TrashSpawnArea.Position.Z
    x,z = math.random(x - r, x + r), math.random(z - r, z + r)
    local pos = Vector3.new(x, TrashSpawnArea.Position.Y, z)
    TrashClone.Parent = workspace
    TrashClone.Position = pos
numb = numb + 1
end
end

Hope this is what you were looking for! Also I'm guessing TrashSpawnArea is a transparent block above the baseplate?

4 comments

0

Yes it is! Thanks! I will try this!

0

I'm guessing "if numb < 3 then" is the limit right?

0

Oh yea, sorry for the late reply ;-;

0

Also if you want the parts to delete themselves after a while, you could try coroutines (thought im not dure if this is the best solution)

This discussion is preserved as it was. New answers, comments, and votes are closed.