I am making a script where you click the gui button then an audio plays and the whole server can hear it. My script dosent work. Any help?
Startergui script
-- LocalScript inside the GUI Button
local button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playSoundEvent = ReplicatedStorage:WaitForChild("PlaySoundEvent")
button.MouseButton1Click:Connect(function()
playSoundEvent:FireServer()
end)
A remote event is placed
Server script service script
-- Script in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playSoundEvent = ReplicatedStorage:WaitForChild("PlaySoundEvent")
playSoundEvent.OnServerEvent:Connect(function(player)
local character = player.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end
-- Create the sound object
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://<YOUR_SOUND_ID>" -- Replace <YOUR_SOUND_ID> with the sound's asset ID
sound.Parent = humanoidRootPart
sound.Volume = 1 -- Adjust volume as needed
sound:Play()
-- Cleanup sound object after it finishes
sound.Ended:Connect(function()
sound:Destroy()
end)
end)