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

How would a script choose a random 'child' inside a folder and then insert it somewhere else?

Asked by 1 year ago

I've been trying to make it so that the script chooses a random skybox and inserts it into "game.lighting". But I can't seem to get it to work. Here's my code. Thanks!

if value.Value == 9 then
    local sky1 = workspace.Skies.Darkness
    local sky2 = workspace.Skies.DeepSpace
    local sky3 = workspace.Skies["Night Sky"]
    local sky4 = workspace.Skies.RedNight
    local sky5 = workspace.Skies.Sky
    local skies = {sky1,sky2,sky3,sky4,sky5}
    local skyduplicat = math.random(1, #skies)
    skyduplicat.Parent = game.Lighting
end

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

You're attempting to parent the number that was returned by the math.random function.

As the math.random method will return a random number from the provided min and max parameters, you can use the returned number to index the object in the table.

local number = math.random(1, #skies)
skies[number].Parent = game.Lighting
Ad

Answer this question