Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to add a limit to a parts spawning script?

Asked by 4 years ago
Edited 4 years ago

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

Log in to vote
1
Answered by 4 years ago

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?

0
Yes it is! Thanks! I will try this! matiss112233 258 — 4y
0
I'm guessing "if numb < 3 then" is the limit right? matiss112233 258 — 4y
0
Oh yea, sorry for the *late* reply ;-; fighterkirbyzx 102 — 4y
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) fighterkirbyzx 102 — 4y
Ad

Answer this question