lobbymusic = script:WaitForChild("lobbymusic") print("Got table 'Lobbymusic!'") local replicatedstorage = game:GetService("ReplicatedStorage") print("Got replicatedstorage!") local statustag = replicatedstorage:WaitForChild("statustag") print("Got statustag!") local electro = script.lobbymusic:WaitForChild("electro") print("Got electro!") local motivational = script:WaitForChild("motivational") print("Got m1!") local rogue = script:WaitForChild("rogue") print("Got m2!") local threestep = script:WaitForChild("threestep") print ("Got threestep!") local playing = script:WaitForChild("musicplaying") print("Got boolean 'Playing!'") lobbytable = lobbymusic:GetChildren() print("Got children of table 'Lobbymusic!'") while true do wait() if playing.Value == true then print("Music is already playing!") elseif playing.Value == false then if statustag.Value == "Error: Not enough players! Invite a friend to start playing!" or statustag.Value == "The game will start soon" and playing.Value == false then playmusic = lobbytable[math.random(1,#lobbytable)]:Play() for vol = 0,0.6,0.05 do wait(0.2) playmusic.Volume = vol end end end end
This basically puts music into a table and then randomly plays one. For line 29 when I try to adjust its volume, however, it gives me this error: Line 29: Attempt to index global 'playmusic,' a nil value
You're setting the variable to the Play method of the sound, you need to set the variable to the sound without the Play method, put the Play method underneath and it should work.
lobbymusic = script:WaitForChild("lobbymusic") print("Got table 'Lobbymusic!'") local replicatedstorage = game:GetService("ReplicatedStorage") print("Got replicatedstorage!") local statustag = replicatedstorage:WaitForChild("statustag") print("Got statustag!") local electro = script.lobbymusic:WaitForChild("electro") print("Got electro!") local motivational = script:WaitForChild("motivational") print("Got m1!") local rogue = script:WaitForChild("rogue") print("Got m2!") local threestep = script:WaitForChild("threestep") print ("Got threestep!") local playing = script:WaitForChild("musicplaying") print("Got boolean 'Playing!'") lobbytable = lobbymusic:GetChildren() print("Got children of table 'Lobbymusic!'") while true do wait() if playing.Value == true then print("Music is already playing!") elseif playing.Value == false then if statustag.Value == "Error: Not enough players! Invite a friend to start playing!" or statustag.Value == "The game will start soon" and playing.Value == false then playmusic = lobbytable[math.random(1,#lobbytable)] playmusic:Play() for vol = 0,0.6,0.05 do wait(0.2) playmusic.Volume = vol end end end end