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

Random song choosing not working properly! Please help?

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!

local Songs = script:FindFirstChild("Songs")
local Gui = game.Lighting:FindFirstChild("ScreenGui")
local Value = 2--change to num songs
while true do
	local Clone =  Songs:Clone()
	Clone.Parent = game:FindFirstChild("Lighting")
repeat
	local ChosenSong = Songs[math.random(1,Value)]--This is the part I am having a problem with!
	if ChosenSong.Name == "Disco" then
		for player, v in pairs(game.Players:GetPlayers()) do
			local Guis = player:FindFirstChild("PlayerGui")
			local Clone2 = Gui:Clone()
			Clone2.Parent = Guis
			
		end
		ChosenSong:Play()
		wait(ChosenSong.TimeLength)
		ChosenSong:Destroy()
		for player, v in pairs(game.Players:GetPlayers()) do
			local Guis = player:FindFirstChild("PlayerGui")
			local Clone2 = Guis:FindFirstChild("ScreenGui")
			
			Clone2:Destroy()
		end
		Value = Value - 1
	else
		ChosenSong:Play()
		wait(ChosenSong.TimeLength)
		ChosenSong:Destroy()
		Value = Value - 1
	end
until Value == 0
Clone.Parent = script
end


1 answer

✓ Accepted answer

You are so close. Here is a (hopefully) correct fix:

local Songs = script:FindFirstChild("Songs"):GetChildren()
local Gui = game.Lighting:FindFirstChild("ScreenGui")
while true do
    local Clone =  Songs:Clone()
    Clone.Parent = game:FindFirstChild("Lighting")
repeat
    local ChosenSong = Songs[math.random(1, #Songs)]
    if ChosenSong.Name == "Disco" then
        for player, v in pairs(game.Players:GetPlayers()) do
            local Guis = player:FindFirstChild("PlayerGui")
            local Clone2 = Gui:Clone()
            Clone2.Parent = Guis

        end
        ChosenSong:Play()
        wait(ChosenSong.TimeLength)
        ChosenSong:Destroy()
        for player, v in pairs(game.Players:GetPlayers()) do
            local Guis = player:FindFirstChild("PlayerGui")
            local Clone2 = Guis:FindFirstChild("ScreenGui")

            Clone2:Destroy()
        end
        Value = Value - 1
    else
        ChosenSong:Play()
        wait(ChosenSong.TimeLength)
        ChosenSong:Destroy()
        Value = Value - 1
    end
until Value == 0
Clone.Parent = script
end

Changes:

  • Line 1: Added ":GetChildren()"
  • Removed "Value" variable, replaced with "#Songs"

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.

    • So if two songs are in the Folder, then "#Songs" = 2.

2 comments

0

Wow thanks!

0

I actually fixed it lol.

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