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

How to use a complicated table?

Asked by 8 years ago

So what I am trying do to is one of the more advanced stuff, like this:

while true do
    local Bricks = game.Workspace.ExplosionBricks:GetChildren()
    local number = math.random(5, #Bricks)
    local Chosen = Bricks[number]
    Chosen.Name = "ExplosionBrick1"
    Chosen.BrickColor = BrickColor.new("Bright red")
    local input = game.Lighting.Script:Clone()
    wait(1)
    local explosion = Instance.new("Explosion")
    explosion.BlastPressure = 100000000
    explosion.ExplosionType = Enum.ExplosionType.CratersAndDebris
    explosion.Position = game.Workspace.ExplosionBricks.ExplosionBrick1.Position
    explosion.Parent = workspace
    input.Parent = explosion
    wait(0.1)
    Chosen.BrickColor = BrickColor.new("Institutional White")
    wait(1)
    Chosen.Name = "ExplosionBrick"
end

So, what I am doing here is I am trying to make 5 parts red, but when I test my game it only makes 1 part red. For this part, I may have to do a for loop.

Question 1: Well, I need someone to tell me if I just returned a table for the 5 chosen parts. Because if I did, it would be easier to do a for loop:

for _, brick in pairs (bricks) do
    brick.BrickColor = BrickColor.new("Bright red")
end

Question 2: Despite the pairs part, I need to find out how to make sure that they are named DIFFERENTLY. That will play an easier part in Question 3. How could I do that? Maybe I should make 5 boolvalues in workspace and name them Name1Taken, Name2Taken, Name3Taken, Name4Taken, Name5Taken Well, I don't know how to do this part, so can you please help?

Question 3: This question will be added when the answer to question 2 comes. Thank you.

1 answer

Log in to vote
0
Answered by 8 years ago
local number = math.random(5, #Bricks)

math.random only returns a single number. You need a loop. Using a function for this will help keep the main logic easy to understand. The function's job will be to get 5 different bricks:

function GetBricks(bricks, n) --Randomly selects n different bricks. The table will be modified in the process.
    local chosen = {}
    for i = 1, math.min(n, #bricks) do
        table.insert(chosen, table.remove(bricks, math.random(1, #bricks)))
    end
    return chosen
end

The big line is the one that starts with table.insert. What am I doing there? Let's start in the middle.

  • math.random(1, #bricks) says to choose a random index from 1 to #bricks
  • table.remove(bricks, math.random(1, #bricks)) removes the chosen brick from the list of bricks (so we don't chose the same brick twice). It also returns the chosen element (ie the chosen brick).
  • table.insert(chosen, table.remove(bricks, math.random(1, #bricks))) takes the chosen brick and adds it to the chosen list

You use it like this (or you can use your pairs loop)

local Chosen = GetBricks(Bricks, 5)
for i = 1, #Chosen do
    Chosen[i].BrickColor = BrickColor.new("Bright red")
end

For your 2nd question, my understanding is that you want to select 5 bricks, each with a unique name. There are a couple ways to do this, but here's one:

function GetBricks(bricks, n) --Randomly selects n different bricks. The table will be modified in the process.
    local chosen = {}
    local brick
    for i = 1, math.min(n, #bricks) do
        brick = table.remove(bricks, math.random(1, #bricks))
        for j = #bricks, 1, -1 do --Remove all bricks with the same name
            if bricks[j].Name == brick.Name then table.remove(bricks, j) end
        end
        table.insert(chosen, brick)
    end
    return chosen
end
Ad

Answer this question