We use cookies on this site to enhance your user experience

Play Local Sound

Play Local Sound

This sample creates and plays a sound locally when a button in a custom Studio plugin is pressed.

local SoundService = game:GetService("SoundService")

-- Create custom plugin button
local toolbar = plugin:CreateToolbar("Empty Script Adder")
local newScriptButton = toolbar:CreateButton("Add Script", "Create an empty Script", "rbxassetid://1507949215")

local function playLocalSound(soundId)
	-- Create a sound
	local sound = Instance.new("Sound")
	sound.SoundId = soundId
	-- Play the sound locally
	SoundService:PlayLocalSound(sound)
	-- Once the sound has finished, destroy it
	sound.Ended:Wait()
	sound:Destroy()
end

local function onNewScriptButtonClicked()
	-- Create new empty script
	local newScript = Instance.new("Script")
	newScript.Source = ""
	newScript.Parent = game:GetService("ServerScriptService")
	-- Call function to play local sound
	playLocalSound("")  -- Insert audio asset ID here
end

-- Connect plugin button to action function
newScriptButton.Click:Connect(onNewScriptButtonClicked)