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

My random music selector is not working? [Resolved]

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

2 comments

0

Shouldn't you add a SoundId?

0

There is a sound ID. Even without a sound ID, it wouldn't return that error.

1 answer

✓ Accepted answer

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

2 comments

0

Oh, right. Thanks!

0

No problem. :)

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