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

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

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

✓ Accepted answer

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

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