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?
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)