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

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

Asked by 1 year ago
Edited 1 year ago

[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)


0
I decided to put the sound in the player's head theking66hayday 841 — 1y

2 answers

Log in to vote
1
Answered by 1 year ago
Edited 1 year ago

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)
0
Ok I will try this rn theking66hayday 841 — 1y
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. theking66hayday 841 — 1y
0
Alright, updated it! Hope this will help! LikeableEmmec 470 — 1y
0
ok I will try it theking66hayday 841 — 1y
View all comments (10 more)
0
You are using the Script, not a LocalScript. You should insert a new LocalScript into StarterGui and add the code stated above. LikeableEmmec 470 — 1y
0
ok let me try that thx theking66hayday 841 — 1y
0
It works, but I'm wanting other players in the area to be able to hear the sound theking66hayday 841 — 1y
0
Sure! Updating the answer now. LikeableEmmec 470 — 1y
0
There you go! Try this code. LikeableEmmec 470 — 1y
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). LikeableEmmec 470 — 1y
0
Okay thank u I will try it out in a bit! theking66hayday 841 — 1y
0
Thank u so much it works! theking66hayday 841 — 1y
1
No problem! Glad to help :) LikeableEmmec 470 — 1y
Ad
Log in to vote
0
Answered by 1 year ago

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)
0
As the MouseButton1Click event does not have any parameters, this will only result in an error as 'player' is nil. xInfinityBear 1777 — 1y
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. theking66hayday 841 — 1y

Answer this question