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 5 years ago
Edited 5 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:

01local TrashSpawnArea = workspace.TrashSpawnArea
02local Trash = workspace.Trash
03local r = 255
04 
05while wait(1) do
06    local TrashClone = Trash:Clone()
07    local x,z = TrashSpawnArea.Position.X, TrashSpawnArea.Position.Z
08    x,z = math.random(x - r, x + r), math.random(z - r, z + r)
09    local pos = Vector3.new(x, TrashSpawnArea.Position.Y, z)
10    TrashClone.Parent = workspace
11    TrashClone.Position = pos
12end

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

01local TrashSpawnArea = workspace.TrashSpawnArea
02local Trash = workspace.Trash
03local numb = 0
04local r = 255
05 
06while wait(1) do
07if numb < 3 then
08    local TrashClone = Trash:Clone()
09    TrashClone.Name = "TheName=/"
10    local x,z = TrashSpawnArea.Position.X, TrashSpawnArea.Position.Z
11    x,z = math.random(x - r, x + r), math.random(z - r, z + r)
12    local pos = Vector3.new(x, TrashSpawnArea.Position.Y, z)
13    TrashClone.Parent = workspace
14    TrashClone.Position = pos
15numb = numb + 1
16end
17end

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 — 5y
0
I'm guessing "if numb < 3 then" is the limit right? matiss112233 258 — 5y
0
Oh yea, sorry for the *late* reply ;-; fighterkirbyzx 102 — 5y
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 — 5y
Ad

Answer this question