Please help! My table is not working properly. It keeps saying that '1 is not a valid member of the folder. IDK why it does the. Help!
01 | local Songs = script:FindFirstChild( "Songs" ) |
02 | local Gui = game.Lighting:FindFirstChild( "ScreenGui" ) |
03 | local Value = 2 --change to num songs |
04 | while true do |
05 | local Clone = Songs:Clone() |
06 | Clone.Parent = game:FindFirstChild( "Lighting" ) |
07 | repeat |
08 | local ChosenSong = Songs [ math.random( 1 ,Value) ] --This is the part I am having a problem with! |
09 | if ChosenSong.Name = = "Disco" then |
10 | for player, v in pairs (game.Players:GetPlayers()) do |
11 | local Guis = player:FindFirstChild( "PlayerGui" ) |
12 | local Clone 2 = Gui:Clone() |
13 | Clone 2. Parent = Guis |
14 |
15 | end |
You are so close. Here is a (hopefully) correct fix:
01 | local Songs = script:FindFirstChild( "Songs" ):GetChildren() |
02 | local Gui = game.Lighting:FindFirstChild( "ScreenGui" ) |
03 | while true do |
04 | local Clone = Songs:Clone() |
05 | Clone.Parent = game:FindFirstChild( "Lighting" ) |
06 | repeat |
07 | local ChosenSong = Songs [ math.random( 1 , #Songs) ] |
08 | if ChosenSong.Name = = "Disco" then |
09 | for player, v in pairs (game.Players:GetPlayers()) do |
10 | local Guis = player:FindFirstChild( "PlayerGui" ) |
11 | local Clone 2 = Gui:Clone() |
12 | Clone 2. Parent = Guis |
13 |
14 | end |
15 | ChosenSong:Play() |
Changes:
Explanation:
First, the Songs variable in your code simply referred to the Folder which the songs are stored in. To rectify this, I added the "GetChildren()" function, which takes all of the Folder's children and places them into a table. That means in my code, the Songs variable is now a table with all of the Folder's songs.
Then, I removed the Value variable, because there is an easy trick to get the length of the Songs table (length meaning number of songs). By putting a '#' in front of the Songs variable, you get the length of the table.