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

How to choose random part from :GetChildren()?

Asked by 6 years ago

Issue
Code gathers all the Tiles from a model, all of which contain a NumberValue called Count and a BoolValue called IsBomb. What i would like to know is how to get a certain number of Tiles from the table.
Code

repeat wait() until game:GetService("ContentProvider").RequestQueueSize <= 0
repStorage = game:GetService("ReplicatedStorage")

if game.Players.LocalPlayer then        
    local grid = repStorage.Grid

    local cGrid = grid:Clone()
    cGrid.Parent = workspace
    cGrid:MoveTo(Vector3.new(0, 0.5, 0))
    for i, v in pairs(cGrid:GetChildren()) do
        if v:FindFirstChild('Count') then
            -- get 5 random tiles
            -- Set IsBomb to true within 5 tiles
        end
    end
end

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago
local children = cGrid:GetChildren()
local tiles = {}
for _,v in pairs(children) do
   if v:FindFirstChild("Count") and v:FindFirstChild("IsBomb") then
      table.insert(tiles,1,v)
   end
end

for i = 1,5 do
   local childNum = math.random(1,#tiles)
   local chosenTile = tiles[childNum]
   --do stuff with chosenTile
   table.remove(tiles,childNum)
end
0
That does not take into account if the part is a tile or not, and just gets all the children of the model pluginfactory 463 — 6y
0
Sorry I misread your post, I've updated the script mattscy 3725 — 6y
Ad

Answer this question