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

New Coder, Help Debugging a Surface GUI That Plays Music?

Hi! I'm just now dabbling in ROBLOX programming (with no prior coding knowledge) so my code will be very crude, so bear with me.

I'm creating a surface GUI that has a large image button in the middle, and two image buttons on either side to cycle the middle image. I don't know the best way to do this, so so far I have a script in the left and right buttons that increases or decreases a _G variable by one every time you click. It looks like this:

script.Parent.MouseButton1Click:connect(function()

_G.song = _G.song + 1

end)

Then I have another script in the big central image button that is supposed to detect if the _G variable equals either 1 or 2, and plays a different song on click depending on the value of the variable. The code looks like this:

mus1=workspace.juke.buttons.music1
mus2=workspace.juke.buttons.music2

while true do

if _G.song == 1 then
	script.Parent.MouseButton1Click:connect(function(one)
	mus1:Play()
	end)
elseif _G.song == 2 then
	script.Parent.MouseButton1Click:connect(function(two)
	mus2:Play()
	end)
end
wait(.25)
end

When I click the big central button however, it plays both songs, not one or the other. Why is it playing both?

1 comment

0

math.random is actually "random" though

1 answer

✓ Accepted answer
local jukeBox = workspace.juke.buttons
local mus1 = jukeBox.music1
local mus2 = jukeBox.music2


-- Make a sound in each button and put the song id inside them

local sound1 = mus1:WaitForChild("Sound") 
local sound2 = mus2:WaitForChild("Sound")

mus1.MouseButton1Click:Connect(function()
	if sound2.Playing then
		sound2:Stop()
	end
	sound1:Play()
end)

mus2.MouseButton1Click:Connect(function()
	if sound1.Playing then
		sound1:Stop() -- Make sure there is a sound there
	end
	sound2:Play()
end)

2 comments

0

Thank you! I had to do a little modifying but it worked! I have one more concern, though. Say I had 10 songs, not 2. Is there a way to stop ALL music from playing, because adding a stop function for each one is impractical.

0

Could use a for loop for all the songs in the model and if they are not named the song that is chosen then you could stop it

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