I have made a GUI that has a simple “Play Button”
I use it to make the assets load faster.
But It seems kind of empty,
It needs music, but how would I get a sound to play a local sound and when a TextButton is Pressed it stops?
I have made a GUI that has a simple “Play Button”
I use it to make the assets load faster.
But It seems kind of empty,
It needs music, but how would I get a sound to play a local sound and when a TextButton is Pressed it stops?
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://your_music_id"
sound.Parent = game:GetService("SoundService") -- or wherever you want.
local button = your_button
button.Activated:Connect(function()
sound:Stop()
--or sound:Destroy() if you don't want to use it again.
end)
Assuming this is placed in a LocalScript inside of a button, go with something like this:
local button = script.Parent
local musicPlaying = false
local audioID = 1843404009 --Replace this with your music ID
local sound = Instance.new("Sound", button)
sound.SoundId = 'rbxassetid://'..audioID
button.MouseButton1Click:connect(function()
if(musicPlaying == false) then
sound:Play() --Music is not playing and the user clicked the button, so let's start the music
musicPlaying = true --We just started playing the music ^, so we want to make sure if they click the button again the music turns off
else
sound:Stop() --Music was playing but the user clicked the button, let's turn it off
musicPlaying = false --Since we just turned off the music, we want to make it so if they click the button again the music starts
end
end)
One thing to note is that if you play the music, stop it, then play it again, it’ll start from the beginning again. If you’d rather have it pause, simply switch the :Play()
to :Resume()
, and switch the :Stop()
to :Pause()
If you have any more questions, just ask and I’ll be glad to answer
A Local Script Will be Inside the Button correct?
add this to line 4.
sound:Play()
Thanks man.
Yes, that’s correct. Create a localscript inside of the button, and put the code into it
it just plays and stuff but it wont stop
local sound = Instance.new(“Sound”)
sound.SoundId = “rbxassetid://2573582104”
sound.Parent = game:GetService(“SoundService”) – or wherever you want.
sound:Play()
local button = script.Parent.Parent.TextButton
button.Activated:Connect(function()
sound:Stop()
–or sound:Destroy() if you don’t want to use it again.
end)
How would we have it if we wanted it to play the music in the background then when we click the button it then stops?
This just fixed a script I’ve been working hours on, thanks!