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

How do i get a Gui to play audio on click?

Asked by 9 years ago
function onClicked()
wait(1)         
script.Parent.GUIAUDIO:play()
wait(1)
end

script.Parent.MouseConnect:connect(onClickedButton)

--This is what i have so far

0
What is script.Parent, is it a GUI button? Spongocardo 1991 — 9y
0
I want a very specific Textbutton to play audio on click BuildingAustin 0 — 9y

3 answers

Log in to vote
2
Answered by 9 years ago

Assuming the script's parent is a GUI button, you can use MouseButton1Click as the event to trigger a function to occur.

What is MouseButton1Click and how do I use it in my script?

MouseButton1Click is an event in a GUI button that is triggered when the left mouse button is clicked. So by assuming that the script's parent is a GUI button, you can change your script to this:

function onClicked()
wait(1)         
script.Parent.GUIAUDIO:play()
wait(1)
end

script.Parent.MouseButton1Click:connect(onClicked) --The function name should be in the parenthesis (brackets) of the connect method.

But what if your GUI button is a child of the script's parent? Well, all you need to do is change the path on the connection line so that it points to your GUI button.

function onClicked()
wait(1)         
script.Parent.GUIAUDIO:play()
wait(1)
end

script.Parent.BUTTON.MouseButton1Click:connect(onClicked) --Change BUTTON to the name of your GUI button (TextButton or ImageButtons are GUI buttons).

Shortening your script:

While the script I provided above will work, you can shorten your code so that it performs better. You can use an anonymous function to make your script shorter.

script.Parent.MouseButton1Click:connect(function()
    wait(1)         
    script.Parent.GUIAUDIO:play()
    wait(1)
end)

Read more on anonymous functions here.

0
Oh, I spent way too long writing this up that Perci posted before me. My bad. Spongocardo 1991 — 9y
0
Still a good answer Perci1 4988 — 9y
0
Thanks. Spongocardo 1991 — 9y
Ad
Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
9 years ago

MouseConnect is not a valid event. If you're not sure about properties, events, etc. always be sure to look it up on the Roblox wiki. The proper event for TextButtons is MouseButton1Click.

Another problem is that the function you try to connect the event to doesn't exist. You named your function onClicked, but you try to connect the event to a function named onClickedButton. This will result in an error. You need to do something like:

script.Parent.MouseButton1Click:connect(onClicked)


Since I don't know the hierarchy, it's possible that MouseConnect is a GUI and you forgot an event entirely. If this is true, you just have to add the event:

script.Parent.MouseConnect.MouseButton1Click:connect(onClicked)
1
Or a very useful spot to look is the Object Browser in ROBLOX Studio. However, learning about them is more of a Wiki thing. alphawolvess 1784 — 9y
0
didnt work BuildingAustin 0 — 9y
0
What exactly "didn't work"? Any output errors? Perci1 4988 — 9y
Log in to vote
-1
Answered by 9 years ago

Thank you very much :)

Answer this question