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

How do I create a script that gets a random child?

Asked by 4 years ago

How do I create a :GetChildren() that randomly picks a child?

2
:GetChildren() returns a table of all the children, so you could do something like thing:GetChildren()[math.random(1, #thing:GetChildren())] which would give you a random child. msuperson24 69 — 4y

1 answer

Log in to vote
3
Answered by
Elixcore 1337 Moderation Voter
4 years ago
Edited 4 years ago
parent = workspace.Model

local rng = math.random(1, #parent:GetChildren())

rng is a random number between 1 and the amount of children the instance has.

If an instance has 5 children, it will be a number from 1 to 5.

With this we can select a random child from the instance.

local child = parent:GetChildren()[rng]
Ad

Answer this question