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

How to make background music play only when clicking the GUI button?

I have a GUI button inside a seat which plays you music when you click the button while sitting. The only problem, right when I join the game, it automatically plays the background songs even when I haven't clicked the GUI button while I was on the seat. How do I fix it?

math.randomseed(tick())
math.random() 

musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080", -- 1 - Let's Groove
    "rbxassetid://142386715", -- 2 - Can't Touch This
    "rbxassetid://276873987", -- 3 - Noob Song
    "rbxassetid://1100823134", -- 4 - At Dawn
    "rbxassetid://142352014" -- 5 Safety Dance
}

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

1 answer

math.randomseed(tick())
math.random() 

musicTable = --Making a table of music / your songs
{
    "rbxassetid://142305080", -- 1 - Let's Groove
    "rbxassetid://142386715", -- 2 - Can't Touch This
    "rbxassetid://276873987", -- 3 - Noob Song
    "rbxassetid://1100823134", -- 4 - At Dawn
    "rbxassetid://142352014" -- 5 Safety Dance
}
local on = false
button.MouseButton1Click:connect(function()
	on = not on
	print("Music is " .. on)
end)
while true do
	if on then
    --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
end

just define "button"

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