Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Solved] How to play sound when GUI button is clicked??

[Solved] Hi, I am trying to script a system to play a sound when the GUI button is clicked. I got a part in the players torso that has an audio in the part. When the button is clicked I want the audio to play its sound. Also I need the sound to be heard from users nearby. What I keep running into is that when the sound plays it isn't the right player the sound is coming from. I need it to specify the player that clicks it.

Code I tried to get working

local Players = game:GetService("Players")

script.Parent.Parent.TextButton.MouseButton1Click:Connect(function(player)

	

	player.Character.Head.TEST:Play()

end)


1 comment

0

I decided to put the sound in the player's head

2 answers

✓ Accepted answer

EDIT 2: This will make other players hear the sound! Just follow the instructions carefully.

Replace your current code inside the LocalScript and replace it with the code below. Dont forget to insert a RemoteEvent in ReplicatedStorage and call it "Sound"!

local p = game.Players.LocalPlayer

p.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("TextButton").MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Sound:FireServer()
end)

Insert a script into ServerScriptService and type in this code:

game.ReplicatedStorage.Sound.OnServerEvent:Connect(function(p)
	p.Character.Head.TEST:Play()
end)

14 comments

0

Ok I will try this rn

0

I just tested and I still get an error. Here is the screenshot of the error in output: https://docs.google.com/document/d/1kIPJ52d0_YT7WM8UWjdziPZ1ZblsWb46SCMwxTENSKs/edit?usp=sharing Also I updated my post with the new changes to the code adding the character part.

0

Alright, updated it! Hope this will help!

0

ok I will try it

0

You are using the Script, not a LocalScript. You should insert a new LocalScript into StarterGui and add the code stated above.

0

ok let me try that thx

0

It works, but I'm wanting other players in the area to be able to hear the sound

0

Sure! Updating the answer now.

0

There you go! Try this code.

0

Incase there isnt a range for the sound, look in the properties of "Test" and look at RollOffMaxDistance. Replace its current number with 50 (meaning anybody within a 50 stud radius will hear it).

0

Okay thank u I will try it out in a bit!

0

Thank u so much it works!

1

No problem! Glad to help :)

you can try as a non local script inside the GUI:

local GUI = script.parent -- creates variable for GUI
local button = GUI.button -- creare variable for GUI button

button.MouseButton1Click:Connect(function(player) -- make sure to customize the button to the way you like it
	player.LowerTorso.sound:Play() -- change 'sound' to the sound name, and the position where it's targeting which is the 'LowerTorso'
end)

2 comments

0

As the MouseButton1Click event does not have any parameters, this will only result in an error as 'player' is nil.

0

It didn't work :( I decided to play around with it . It still doesn't work I updated my post to show what I did.

This discussion is preserved as it was. New answers, comments, and votes are closed.