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