PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

PlayLocalSound

Plays a Sound locally, meaning the sound will only be heard by the client calling this function, regardless of where it’s parented to. This function is most useful for playing a Sound locally in the Studio client, for instance in a Script for a Articles/Intro to Plugins|custom plugin.

Parameters

Name Type Default Description

The Sound to be played.

Returns

Return Type Summary

Code Samples


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)