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

How do you find a part using math.random?

I'm trying to find a certain block out of 3 blocks, using math.random

local FolderForParts = game.Workspace.Folder
local PartNumber = math.random(1,3) -- 3 Blocks in folder
local Part = 				--Finds the part

The part names (just keeping it simple): Part1 Part2 Part3

2 answers

✓ Accepted answer

You need to get the number of the part

local FolderForParts = game.Workspace.Folder
local PartNumber = math.random(1,#FolderForParts:GetChildren())
local Part = FolderForParts:WaitForChild("Part".. tostring(partNumber))

This script transforms the selected number into a string and gets the selected part in the folder. P.S: With this script you can put as many parts as you want as long as they are numbered

I think this is what you want.

local FolderForParts = game.Workspace.Folder:GetChildren() -- make a table of parts
local PartNumber = math.random(1,#FolderForParts) -- 1 through length of table
local Part = FolderForParts[PartNumber] -- reference chosen part

print(Part.Name)

2 comments

0

or FolderForParts:FindFirstChild('Folder' .. tostring(PartNumber)) if you want the naming and number to be consistent

0

Also a fine way to go about it, but lets keep it simple if hes unsure how to do it in the first place!

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