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

how do i limit how many bricks can be spawned?

i'm making some stupid brick factory game thing and i need to add a limit to how many bricks can be spawned at once (not in general) so i can avoid people lagging the server

you spawn bricks with the press of a button, anyway heres the script

local part = script.Parent
local h = 0.5
local ohno = 60
local db = false

part.ClickDetector.MouseClick:connect(function(player)
	
	local bruh = Instance.new("Part", workspace)
	bruh.Anchored = false
	bruh.Position = game.Workspace.jeff.Position
	bruh.Material = Enum.Material.Brick
	bruh.BrickColor = BrickColor.Random()
	bruh.Transparency = 0
	bruh.Size = Vector3.new(2, 2, 2)
	part.BrickColor = BrickColor.new("Really red")
	wait(h)
	db = true
	part.BrickColor = BrickColor.new("Lime green")
	wait(h)
	db = false
  	wait(ohno)
	bruh:Destroy()
end)

i'm thinking of doing an if statement but i was like nah

3 answers

What I would do, I know it’s probably not the best way but, I’d just put a timer so like every minute or something it clears the bricks. You can also add a recharge on the tool so that you have to wait like 2 seconds or something before spawning another brick. Tell me iif you need help with code! Hope you are well.

1 comment

0

i've already added a brick removal thing, line 21 and 22

An easy solution (there are lots of solutions to this) would be to put them all in the same folder then use getChildren to return a list of arts and count them.


local folder = Instance.new('Folder')
folder.Name = 'parts'
folder.Parent = workspace

local partLimit = 10

part.ClickDetector.MouseClick:connect(function(player)
	if #folder:GetChildren() > partLimit then return end -- do not run if there are more parts than the limit

	-- normal code .....
end)

I hope this helps. Please comment if you have any other questions about this code.

3 comments

0

As I said, my way isn’t good lol. That sounds like a good method though!

0

hmmm.. doesn't seem to work but i'm going to try to fix it, perhaps it's because the button is in a model or something like that

0

Check that you are parenting the parts to the folder.

Well if you ask me, I will add an "IntValue" into the part and do it this way:

local part = script.Parent
local h = 0.5
local ohno = 60
local db = false
local IntValue = part:WaitForChild("IntValue")

part.ClickDetector.MouseClick:connect(function(player)
	if IntValue.Value <= 2 then -- Once the IntValue reached a certain value, the following codes below will not run (feel free to change "2" to the brick limit you want)
		IntValue.Value = IntValue.Value + 1 -- every time when a player clicks, the int value will go up by 1
		local bruh = Instance.new("Part", workspace)
		bruh.Anchored = false
		bruh.Position = game.Workspace.jeff.Position
		bruh.Material = Enum.Material.Brick
		bruh.BrickColor = BrickColor.Random()
		bruh.Transparency = 0
		bruh.Size = Vector3.new(2, 2, 2)
		part.BrickColor = BrickColor.new("Really red")
		wait(h)
		db = true
		part.BrickColor = BrickColor.new("Lime green")
		wait(h)
		db = false
		wait(ohno)
		bruh:Destroy()
	end
end)

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