Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to choose random part from :GetChildren()?

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

✓ Accepted answer
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

2 comments

0

That does not take into account if the part is a tile or not, and just gets all the children of the model

0

Sorry I misread your post, I've updated the script

This discussion is preserved as it was. New answers, comments, and votes are closed.