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

How do I find a random child of something and use it?

Asked by
Scaii_0 145
6 years ago

So I want to find a child of a folder randomly and then use the position of that part. How would you do this?

So far I have:

local parts = game.Workspace.PartsModel:GetChildren()

Now I need to try find ONE child of that at random. How would I do this?

2 answers

Log in to vote
1
Answered by
Filipalla 504 Moderation Voter
6 years ago
Edited 6 years ago

Im writing this on my phone soo it might not be 100% right.

local randomchild = math.random(1,#parts) --choose random index number
parts[randomchild] --use that index number to get the instance with that number

If you have any problem with the code above Please tell me

Ad
Log in to vote
1
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

We can use math.random() for this.

local Children = game.Workspace.PartsModel:GetChildren()
local RandomIndex = math.random(1, #Children)
local RandomChild = Children[RandomIndex]
print(RandomChild.Name)

Answer this question