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

StarterGui button won't stop music for LocalPlayer?

local Player = game.Players.LocalPlayer.PlayerGui
local theScript = game.StarterGui.Music.musicMan.LocalScript
local button = game.StarterGui.Music.musicMan.ImageButton
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

local function playTheMusic()
	while true do
		musicTable=RandomizeTable(musicTable)
			for i,v in pairs(musicTable) do
				v:Play()
				if v == wetHands then
					wait(wetHands.TimeLength)
				elseif v == sweden then
					wait(sweden.TimeLength)
				elseif v == subwooferLullaby then
					wait(subwooferLullaby.TimeLength)
				elseif v == miceOnVenus then
					wait(miceOnVenus.TimeLength)
				elseif v == mc then
					wait(mc.TimeLength)
				elseif v == livingMice then
					wait(livingMice.TimeLength)
				elseif v == haggstrom then
					wait(haggstrom.TimeLength)
				elseif v == dryHands then
					wait(dryHands.TimeLength)
				elseif v == danny then
					wait(danny.TimeLength)
			end
		end
	end
end

playTheMusic()

local function stopAndstart()
	if Player.danny.IsPlaying then
		danny:Stop()
	elseif
		Player.sweden.IsPlaying then
		sweden:Stop()
	elseif
		Player.wetHands.IsPlaying then
		wetHands:Stop()
	elseif
		Player.subwooferLullaby.IsPlaying then
		subwooferLullaby:Stop()
	elseif
		Player.miceOnVenus.IsPlaying then
		miceOnVenus:Stop()
	elseif
		Player.mc.IsPlaying then
		mc:Stop()
	elseif
		Player.livingMice.IsPlaying then
		livingMice:Stop()
	elseif
		Player.haggstrom.IsPlaying then
		haggstrom:Stop()
	elseif
		Player.dryHands.IsPlaying then
		dryHands:Stop()
	elseif
		Player.danny.IsPlaying then
		danny:Stop()
	else
		playTheMusic()
	end
end

button.MouseButton1Click:connect(stopAndstart())

2 answers

This doesn't really answer your question, but its more of how to shorten your gigantic script. A simple audio playing script shouldn't take up 110 lines.

First of all, why are you creating all of the sounds from scratch, at the top of the script. Cant you just have a folder where you keep all your sounds? And instead of creating a music table out of all thouse, you could just have local musictable=musicfolder:GetChildren()

And at your function playthemusic(), you dont need a plethora of if statements.You never need a plethora of if statements. Just replace the entire function with

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

Its the same thing with the function stopAndstart, it just does the same thing to each of the sounds. You can replace that entire function with

local oneplaying=false

local function stopAndstart()
	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
		playTheMusic
	end
end

I hope this helped reorganize your script.

2 comments

0

I'm certainly new at scripting and efficiency is indeed key, thank you very much for this!

0

your welcome

Your key problem being is that when the function playTheMusic() is called, it is running in a loop.

Once you run something in a loop, especially when using while true do, it will not end.

This is where coroutines come into use.

Coroutines create a separate thread where you can run your loops without disturbing the flow of your main script.

There are 2 key functions that you require: coroutine.resume(c) - c being a created coroutine coroutine.create(f) - f being a function

You must create a coroutine before using it (by calling resume()) There are a couple of ways of making a coroutine, I will show you one.

coroutine.resume(coroutine.create(function() 
 while true do
  -- Your looped script
 end
end))

Now, for your playTheMusic() function, add the above script instead then do your loop. When you then call for a stop, it should work.

Hope this helps!

For more understanding of coroutines, visit Roblox's Wiki Article on it here

EDIT: So your script should go like this:

local function playTheMusic()
	coroutine.resume(coroutine.create(function() 
		while true do
			-- your statement that you currently have
		end
	end)
end

2 comments

0

Could you rewrite my script using coroutines? I am completely stumped by them, I even looked on the wiki and cannot figure them out.

0

Have a look at the edit maybe?

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