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

How do I add default volume to this script?

I made a GUI button, and inside I have an audio which is set to "volume = 0". Inside the audio I added this script;

math.randomseed(tick())
math.random() -- throw out first not-very-random value (known limitation of lua)

musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080",  -- 1 - Let's Groove
    "rbxassetid://671389213",  -- 2 - Can't Touch This
    "rbxassetid://609246843",  -- 3 - Take on Me
    "rbxassetid://1100823134", -- 4 - At Dawn
    "rbxassetid://681266896",  -- 5 - Safety Dance
    "rbxassetid://323743169",  -- 6 - Working for the Weekend
    "rbxassetid://442656036",  -- 7 - Don't you (Forget About Me)
    "rbxassetid://706699539",  -- 8 - Beat it
    "rbxassetid://838691861"   -- 9 - Hey you, you better keep walking!
}

while true do
    --Shuffle
    local index = math.random(1, #musicTable - 1) -- choose an item not from the end
    musicTable[1], musicTable[index] = musicTable[index], musicTable[1] -- switch with first item
    for i = 2, #musicTable-1 do -- do regular shuffle algorithm starting at 2nd item
        index = math.random(i, #musicTable)
        musicTable[i], musicTable[index] = musicTable[index], musicTable[i]
    end
    --Play music
    for i = 1, #musicTable do
        wait(1) -- Just creating a small pause before a random song plays
        script.Parent.SoundId = musicTable[i] --Choose song
        script.Parent:Play() --Play song
        script.Parent.Ended:wait() -- Waiting until the song has finished
    end
end

function

When I set audio to "volume = 1", it starts playing in background without me ever pressing the GUI button. How do I make it so the music starts playing only when clicking the button and prevent the music playing in background randomly unless you click the GUI button?

2 comments

0

dude download a local copy of the songs to your computer before playing it

0

What do you mean download local copy?

1 answer

Hi, I noticed that your script doesn't have a clicked event, only an infinite while loop. To fix this I would try to wrap the while loop up in a MouseButton1Down event. Also, I'm not sure why you have "function" at the bottom of the script. This should work, but I would suggest looking to having a method for turning off the autoplaying music once it's started. This can be done with a boolean variable for whether it's playing or not and while playing do for the while loop instead.

math.randomseed(tick())
math.random() -- throw out first not-very-random value (known limitation of lua)

musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080",  -- 1 - Let's Groove
    "rbxassetid://671389213",  -- 2 - Can't Touch This
    "rbxassetid://609246843",  -- 3 - Take on Me
    "rbxassetid://1100823134", -- 4 - At Dawn
    "rbxassetid://681266896",  -- 5 - Safety Dance
    "rbxassetid://323743169",  -- 6 - Working for the Weekend
    "rbxassetid://442656036",  -- 7 - Don't you (Forget About Me)
    "rbxassetid://706699539",  -- 8 - Beat it
    "rbxassetid://838691861"   -- 9 - Hey you, you better keep walking!
}

script.Parent.Parent.MouseButton1Click:Connect(function()
	while true do
	    local index = math.random(1, #musicTable - 1)
	    musicTable[1], musicTable[index] = musicTable[index], musicTable[1]
	    for i = 2, #musicTable-1 do
	        index = math.random(i, #musicTable)
	        musicTable[i], musicTable[index] = musicTable[index], musicTable[i]
	    end
	    for i = 1, #musicTable do
	        wait(1)
	        script.Parent.SoundId = musicTable[i]
	        script.Parent:Play()
	        script.Parent.Ended:wait()
	    end
	end
end)

3 comments

0

It doesn't work, my music doesn't play at all. :(

0

I'm trying to make like a radio, where you like have music player, and skip buttons. As you click skip buttons, it plays you different songs in one audio kinda like radio station. I do have various scripts for the GUI, let me known which script you need to look at and I will show you. :)

0

Or I can send you model of GUI that you can look at. I hope I'm making this as easiest as possible.

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