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