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

How can i make Lua choose a random Part in a Table?

Asked by
KenUSM 53
6 years ago

I've hit a wall when it comes to this area. What ever I do it always shows the Serial Number for the Part. example: (if I had 2 Parts in my Array, it would say) table: 1F801F34, table:0D1C71AC. Every time I tested it the Serial number would always change never the same. The Best Code I could come up with trying to remove the other tables possibly is.

    local AParts = {ChoosenMap:GetChildren()}
    local SParts = AParts
    if Player.TeamColor == BrickColor.new("Bright blue") then
        local Selecting = SParts[math.random(1,#SParts)]
        table.remove(Selecting,SParts)
        print (Selecting)
    end

The Problem with this code is with the table.remove It would always say "expected Number, got Table" and when I switched the placement of the items (SParts, Selecting) it would say "expected Table got Number". I'm almost about to give up on the whole thing cause this is one of the many functions I am trying to complete and this one is essential for it to work.

0
try changing 'table.remove(Selecting,SParts)' to 'table.remove(Selecting,SParts[Selecting])' so you get the index of the table and not just the table itself Senor_Chung 210 — 6y
0
Yea that helps making showing the index, but i still don't know how to make only 1 Part appears instead of all of them KenUSM 53 — 6y
0
You are putting a table in a table, and not putting anything in the first table. Thats inefficient. It's like doing "Vector3.new(Vector3.new(1, 1, 1))". hiimgoodpack 2009 — 6y

2 answers

Log in to vote
1
Answered by
XAXA 1569 Moderation Voter
6 years ago
Edited 6 years ago

:GetChildren() already returns an array of parts. No need to wrap it inside of another table.

Instead of

local AParts = {ChoosenMap:GetChildren()}

You can simply do

local AParts = ChoosenMap:GetChildren()

Also, for line 2, keep in mind that you're just assigning SParts a reference to AParts. In other words, modifying (insert/remove) SParts will also modify AParts. This may or may not be what you intended.

Lastly, table.remove (in its simplest form) requires a table and an index as its arguments, and returns the entry that was removed.

Instead of

    local Selecting = SParts[math.random(1,#SParts)]
    table.remove(Selecting,SParts)
    print (Selecting)

You should instead do

    local selectedIndex = math.random(#SParts)
    local Selecting = table.remove(SParts, selectedIndex)
    print (Selecting)
0
It reveals the index even trying with your help i still can't find a way to isolate the random chosen Part that would appear first before it goes through the rest. KenUSM 53 — 6y
0
It is #SParts[1], otherwise it would always be one. hiimgoodpack 2009 — 6y
0
Can't do #SParts[1] it will return error "attempt to get length of field '?' (a userdata value)" KenUSM 53 — 6y
Ad
Log in to vote
0
Answered by
KenUSM 53
6 years ago

I've found the solution thanks to you guys, Thank you.

local ChoosenMap = script.Parent.Maps.Map1

game.Players.PlayerAdded:connect(function(Player)

     Part = ChoosenMap:GetChildren()

    for i,Parts in pairs(Part) do 
        if Part.Troops == nil then 
            script.Parent.Folder.Troops:Clone().Parent = Parts
            script.Parent.Folder.ClickDetector:Clone().Parent = Parts
            script.Parent.Folder.BillboardGui:Clone().Parent = Parts
            script.Parent.Folder.SurfaceGui:Clone().Parent = Parts
            script.Parent.Folder.Choosen:Clone().Parent = Parts
            Parts.BillboardGui.Enabled = true
        else 
            return nil
        end

        if Player.TeamColor == BrickColor.new("Bright blue") then
            SelectSpawn()
        end
    end
    createTroops()
end)

function SelectSpawn ()
    local AParts = ChoosenMap:GetChildren()
    local SelectedI = math.random(1,#AParts)
    local Selecting = table.remove(AParts, SelectedI)
    local Select = {Selecting}
        print(Select)
    for i , Selected in pairs(Select) do
         Selected:WaitForChild("SurfaceGui").Frame.BackgoundColor3 =    Color3.new(255,0,0)
    end
end

well, all that's needed now is finding a way I can find SurfaceGui. Thanks Guys.

Answer this question