PlayLocalSound plays to everyone in the server

I’m trying to make a LocalScript that plays a sound to whoever touches a specific part.
The localscript is in StarterPlayerScripts:

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://190619411"
sound.Parent = workspace
local trigger = game.Workspace.klaxontrigger
local isplaying = false
local SoundService = game:GetService("SoundService")

local function playLocalSound(soundId)
	if isplaying then
		return
	end
	local sound = Instance.new("Sound")
	sound.SoundId = soundId
	sound.Volume = 1
	SoundService:PlayLocalSound(sound)
	sound.Ended:Wait()
	sound:Destroy()
	isplaying = false
end

local function onTouch(part)
	playLocalSound(190619411)
end

trigger.Touched:Connect(onTouch)

Whenever someone touches the part, the sound can be heard for the whole server, while I want it only to play for the person that just touched it.

1 Like
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://190619411"
sound.Parent = workspace
local trigger = game.Workspace.klaxontrigger
local isplaying = false
local SoundService = game:GetService("SoundService")
local plr = game.Players.LocalPlayer

local function playLocalSound(soundId)
    if isplaying then
	    return
    end
local sound = Instance.new("Sound")
sound.SoundId = soundId
sound.Volume = 1
SoundService:PlayLocalSound(sound)
sound.Ended:Wait()
sound:Destroy()
isplaying = false
end

local function onTouch(part)
     if part.Parent.Name == plr.Name then
    playLocalSound(190619411)
end
  end

trigger.Touched:Connect(onTouch)

All clients can detect when a part was touched, meaning that all clients are going to play the sound.

1 Like

Make sure that when the part is touched, it is the local player that touched it. Right now this fires whenever any player steps on the part.

1 Like

For some reason, when the script created the new Sound, it wouldn’t load the ID.
So, instead, I made a sound in SoundService and called to play that instead of creating a new Sound.
Here’s what worked:

local trigger = game.Workspace.klaxontrigger
local isplaying = false
local SoundService = game:GetService("SoundService")
local plr = game.Players.LocalPlayer

local function playLocalSound(soundId)
    if isplaying then
	    return
    end
isplaying = true
local sound = SoundService.klaxonbeat
SoundService:PlayLocalSound(sound)
sound.Ended:Wait()
isplaying = false
end

local function onTouch(part)
	if part.Parent.Name == plr.Name then
    	playLocalSound(190619411)
	end
end

trigger.Touched:Connect(onTouch)

Thank you both for your help!

3 Likes