how do you make music play in a screengui? for example, i'm making a game and i have a title screen with labels and buttons, right? and then, i want to add music that lasts until you select the button to play the game! which has it's script that works already
PLAYGAME.MouseButton1Click:Connect(function() Frame.Visible = false end)
then, i try to create the music part, but it doesn't work
01 | --variables-- |
02 | Sound = script.Parent.Sound |
03 | PLAYGAME = script.Parent.PLAYGAME |
04 |
05 | --scripts-- |
06 | Sound:Play() |
07 | Sound.Playing = true |
08 | PLAYGAME.MouseButton 1 Click:connect( function () |
09 | Sound.Playing = false |
10 | end ) |
if im doing something wrong then please tell me that would really help! thanks!
edit: the PLAYGAME button is inside of a frame inside of a ScreenGui and inside of the StarterGui folder. the Sound, i moved to the workspace
01 | --services-- |
02 | local Players = game:GetService( 'Players' ) -- This is the folder that you can see on the explorer. |
03 |
04 | --variables-- |
05 | local Player = Players.LocalPlayer -- This is you, but you can do it just in a LocalScript. |
06 | local PlayerGui = Player:WaitForChild( 'PlayerGui' ) |
07 | local ScreenGui = PlayerGui:WaitForChild( 'ScreenGui' ) -- Just put the name of your ScreenGui here. |
08 |
09 | local Sound = ScreenGui:WaitForChild( 'Sound' ) |
10 | local PLAYGAME = ScreenGui:WaitForChild( 'PLAYGAME' ) |
11 |
12 | --scripts-- |
13 | Sound:Play() |
14 | PLAYGAME.MouseButton 1 Click:Connect( function () -- connect is deprecated use Connect. |
15 | Sound.Playing = false |
16 | end ) |
If you have any question put it below on commentaries.
Sound Objects can only operate in a physical environment, i.e workspace. If you do this via a LocalScript (assuming your code is local), the sound will only be played in your client.
So It would be something like:
01 | local Sound = workspace.Sound |
02 | Sound.SoundID = "rbxassetid://SoundIdHere" |
03 |
04 | local PLAYGAME = script.Parent.PLAYGAME |
05 |
06 | Sound:Play() -- Start the music |
07 |
08 | PLAYGAME.MouseButton 1 Click:connect( function () |
09 | Sound:Stop -- It’s best to use these methods to start/stop music |
10 | end ) |