Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I play a sound on MouseButton1Down on a GUI?

Asked by 9 years ago

I have the following script for a GUI and I want to use this sound http://www.roblox.com/asset/?ID=142448886

textButton = script.Parent
textButton.MouseButton1Down:connect(function()
    if textButton.Text == "Open" then
        textButton.Text = "Close"
         textButton.BackgroundColor3 = Color3.new(1, 0, 0)
    elseif textButton.Text == "Close" then
            textButton.Text = "Open"
             textButton.BackgroundColor3 = Color3.new(0, 1, 0)
    end
end)

1 answer

Log in to vote
1
Answered by 9 years ago
local NAMEHERE = Instance.new("Sound") -- creates a new sound if you dont have it already
NAMEHERE.SoundId = "http://www.roblox.com/asset/?id=142448886" -- inserts the id you gave me
NAMEHERE.Parent = script.Parent -- puts the sound in the gui

textButton = script.Parent - your script
textButton.MouseButton1Down:connect(function()
    if textButton.Text == "Open" then
        textButton.Text = "Close"
        NAMEHERE:Play() -- or :Stop() my script
         textButton.BackgroundColor3 = Color3.new(1, 0, 0)
    elseif textButton.Text == "Close" then
            textButton.Text = "Open"
            NAMEHERE:Play() -- or :Stop() my script
             textButton.BackgroundColor3 = Color3.new(0, 1, 0) -- your script
    end
end)

Ad

Answer this question