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

how do i limit how many parts can my script spawn at once?

Asked by 3 years ago

hi hello im dumb and i cannot script so i'm asking you people that are way better at this than me for some tiny bit of help, i want to make my game not lag by limiting how many parts the player can spawn at once. i have no clue what i'm doing

local part = script.Parent
local brick = Instance.new("Part", workspace)
local parts = 0

while parts < 30 do

part.ClickDetector.MouseClick:connect(function(player)
    parts.Value = parts.Value + 1
    brick.Position = game.Workspace.jeff.Position
    brick.Material = Enum.Material.Brick
    brick.BrickColor = BrickColor.Random()
    brick.Transparency = 0
    brick.Size = Vector3.new(2, 2, 2)
    part.BrickColor = BrickColor.new("Really red")  -- this changes the color of the button that player presses to make the bricks
    wait(0.1)
    part.BrickColor = BrickColor.new("Lime green")
    wait(0.1)
    wait(30)  -- waits a certain amount of time and then destroys the bricks for example if theres too many at once
    brick:Destroy()
    end)
end

any help would be gladly appreciated i'm losing my mind over this it's probably just a quick easy thing but i'm too incompetent for scripting ok byebye!!

0
Hey green Im going to join add you on roblox so I can help you out M9F 94 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Try this

local part = script.Parent.Parent
local brick = Instance.new("Part", workspace)
local parts = 0


    part.ClickDetector.MouseClick:connect(function()
        parts = parts + 1
        if parts <= 30 then
            brick.Position = game.Workspace.jeff.Position
            brick.Material = Enum.Material.Brick
            brick.BrickColor = BrickColor.Random()
            brick.Transparency = 0
            brick.Size = Vector3.new(2, 2, 2)
            part.BrickColor = BrickColor.new("Really red")
            wait(0.1)
            part.BrickColor = BrickColor.new("Lime green")
        wait(0.1)

        while true do
            wait(1)
            if parts >= 30 then
                brick:Destroy()
            end
        end
    end
end)
Ad
Log in to vote
0
Answered by
Vathriel 510 Moderation Voter
3 years ago
while parts < 30 do

This line of code only serves to endlessly reconnect the event, which is a bad thing to do.

Instead you could just use the event, but add an if statement inside of the event.

local part = script.Parent
local brick = Instance.new("Part", workspace)
local parts = 0


part.ClickDetector.MouseClick:connect(function(player)
    if parts > 30 then return end
    parts = parts + 1
    brick.Position = game.Workspace.jeff.Position
    brick.Material = Enum.Material.Brick
    brick.BrickColor = BrickColor.Random()
    brick.Transparency = 0
    brick.Size = Vector3.new(2, 2, 2)
    part.BrickColor = BrickColor.new("Really red")  -- this changes the color of the button that player presses to make the bricks
    wait(0.1)
    part.BrickColor = BrickColor.new("Lime green")
    wait(0.1)
    wait(30)  -- waits a certain amount of time and then destroys the bricks for example if theres too many at once
    brick:Destroy()
    parts = parts - 1
end)

You have a couple of other problems, like you need to decrease the number of parts (so I added that in my example) and you also forgot that for variables like parts, you don't need .Value

Good luck, Vathriel

Answer this question