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

Looped music script won't stop when GUI button clicked?

To be more clear, what I hope to do is make a local gui with local music that stops once the player clicks the button and replays from the begging of the table (musicTable) when the player clicks the button again. I do not know why this is working, I understand that this is a repost but I have not been given an answer before..

local Player = game.Players.LocalPlayer.PlayerGui
local theScript = game.StarterGui.Music.musicMan.ImageButton.LocalScript
local button = script.Parent
local wetHands = Instance.new("Sound", Player)
local sweden = Instance.new("Sound", Player)
local subwooferLullaby = Instance.new("Sound", Player)
local miceOnVenus = Instance.new("Sound", Player)
local mc = Instance.new("Sound", Player)
local livingMice = Instance.new("Sound", Player)
local haggstrom = Instance.new("Sound", Player)
local danny = Instance.new("Sound", Player)
local dryHands = Instance.new("Sound", Player)

haggstrom.SoundId = "http://www.roblox.com/asset/?id=559471050"
livingMice.SoundId = "http://www.roblox.com/asset/?id=559470478"
mc.SoundId = "http://www.roblox.com/asset/?id=559470109"
miceOnVenus.SoundId = "http://www.roblox.com/asset/?id=559469730"
wetHands.SoundId = "http://www.roblox.com/asset/?id=559730038"
sweden.SoundId = "http://www.roblox.com/asset/?id=559534950"
subwooferLullaby.SoundId = "http://www.roblox.com/asset/?id=559469065"
danny.SoundId = "http://www.roblox.com/asset/?id=560091855"
dryHands.SoundId = "http://www.roblox.com/asset/?id=560091413"
danny.Name = "Danny"
dryHands.Name = "Dry Hands"
wetHands.Name = "Wet Hands"
sweden.Name = "Sweden"
subwooferLullaby.Name = "Subwoofer Lullaby"
miceOnVenus.Name = "Mice on Venus"
mc.Name = "MC"
livingMice.Name = "Living Mice"
haggstrom.Name = "Haggstrom"

musicTable = {wetHands, sweden, subwooferLullaby, miceOnVenus, mc, livingMice, haggstrom, danny, dryHands}

local function RandomizeTable(tbl)
	local returntbl={}
	if tbl[1]~=nil then
		for i=1,#tbl do
			table.insert(returntbl,math.random(1,#returntbl+1),tbl[i])
		end
	end
	return returntbl
end

coroutine.resume(coroutine.create(function()
    while true do
        musicTable=RandomizeTable(musicTable)
            for i,v in pairs(musicTable) do
                v:Play()
        wait(v.TimeLength)
        		end
			end
end))

local oneplaying=false
 
local function stopandstart()
   while true do
    for i,v in pairs(Player:GetChildren()) do
        if v:IsA("Sound") then
            if v.IsPlaying then
                v:Stop()
				oneplaying=true
            end
        end
    end
    if oneplaying==false then
		
    end
  end
end


button.MouseButton1Click:connect(function(stopandstart)
end)

1 comment

0

Don't forget to mark this question as answered if my answer helped you, bud! Have a nice day! ;)

1 answer

Hey batistapowerbombtla!

It would appear that the function you're connecting to button's MouseButton1Click event does not do anything. Note that you put an anonymous (unnamed) function inside connect's argument, rather than hook up the actual stopandstart function to the event.

In order for this to work, MouseButton1Click needs to call stopandstart, not an anonymous function. Write the end bit like so:

button.MouseButton1Click:connect(stopandstart);

Now, with that said, I'd like to point out a few ways you can better write this script:

#1) Instead of manually making each sound, make a Dictionary containing all the SoundIds, and have its keys be the names of the songs.

Like so:

-- A dictionary containing each song. The keys correspond to the correct name.
local Music = {
	["Haggstrom"]	= "http://www.roblox.com/asset/?id=559471050";
	["Living Mice"]	= "http://www.roblox.com/asset/?id=559470478";
	-- Rinse and repeat for all other songs.
};

#2) Then build a new playlist each iteration. You'll see why in a moment.

-- Returns a playlist.
local function MakePlaylist()
	local playlist = {};
	
	for songName, ID in pairs(Music) do
		table.insert(
			Playlist,
			{
				Name	= songName;
				SoundID	= ID;
			};
		);
	end;

	return playlist;
end;

#3) To play all the songs in Music continually, connect to RunService.Stepped, as opposed to using a while loop.

-- The sound that will play the current SoundId
local Sound			= Instance.new("Sound", Player);
local LoopIsActive		= false;
local MusicIsPlaying		= false;

-- Play each song in the list, but play no song twice.
local function PlayMusic()
	local playlist = MakePlaylist();
	
	for i = 1, #playlist do
		local connection;
		local index		= math.random(#playlist);

		Sound.SoundId	= playlist[index].SoundID;

		-- I'm going to assume you might want to display the song name on the GUI. Replace 'SoAndSo' with the variable pointing to the GUI object and uncomment. ;)
		-- SoAndSo.Text		= "Now playing: " .. playlist[index].Name;

		MusicIsPlaying = true;

		-- Wait until the new SoundId is loaded.
		Sound.Loaded():Wait();

		Sound:Play();

		connection = Sound.Ended:Wait();

		connection:Disconnect();

		MusicIsPlaying = false;
		
		-- We remove the current SoundId from the table to prevent it from playing twice
		table.remove(playlist, index);
	end;
	
end;

-- Runs about 30 times/second. Restarts the music loop if it is not active.
game:GetService("RunService"):Connect(function()
	if (not LoopIsActive) then
		LoopIsActive = true;

		PlayMusic();

		LoopIsActive = false;
	end;
end);

-- Whenever the button is clicked, invert MusicIsPlaying and set Sound.Playing equal to it.
button.Clicked:Connect(function()
	MusicIsPlaying = not MusicIsPlaying;

	Sound.Playing = MusicIsPlaying;
end);

I scribbled that up during a lull in the evening, and did not have a decent opportunity to test what I wrote, so no guarantees it will work. Do let me know if you run into any problems.

Have a good one, batistapowerbombtla, and best of luck making your game!

Best wishes, tkcmdr

Edit: Corrected Typos, Added connection to the button click (Dim oversight. My bad.)

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