How to script random button sounds?

Hey there! I’ve recently tried to script a button where a random sound from a list would play, each time it’s pressed. I created a list of three sound effects, defined services and variables, etc, but I’m really stuck on how to actually execute my code when the button is clicked. Here’s my full button script below;

--Call Roblox SoundService
local soundService = game:GetService("SoundService")

--Define Parent Button
local btn = script.Parent

--Define Individual Sounds
local relayClicks4_SFX = soundService["Relay Clicks 4 (SFX)"]
local relayClicks9_SFX = soundService["Relay Clicks 9 (SFX)"]
local relayClicks17_SFX = soundService["Relay Clicks 17 (SFX)"]

--Define Further Variables
local soundList = {relayClicks4_SFX, relayClicks9_SFX, relayClicks17_SFX}

--Execute Selection Code
--??? ??? ???

As you can see, I have everything defined, but I just can’t quite put it together. I would appreciate some help on the last part of my script! :heart:

Hello, You can use the below code to make your code above execute when the button is clicked using MouseButton1Click

script.Parent.MouseButton1Click:Connect(function()
-- replace this with the code you have listed above.
end)

Hmm, that won’t do anything, because inside that event I’d just be defining everything but not actually putting it together.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.