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

How do you find a part using math.random?

Asked by 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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

Ad
Log in to vote
0
Answered by 5 years ago

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)
0
or FolderForParts:FindFirstChild('Folder' .. tostring(PartNumber)) if you want the naming and number to be consistent User#26471 0 — 5y
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! DinozCreates 1070 — 5y

Answer this question