Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I play a sound locally?

Asked by
TudiPYM 12
3 years ago

So I wanna play a sound locally when the client touches a part. I have read the PlayLocalSound() article on the dev forum but didnt understand much, could someone give me an example code?

1 answer

Log in to vote
0
Answered by
kylaex 17
3 years ago

There's a script sample already created for this. Here it is:

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)

https://devforum.roblox.com/t/playlocalsound-plays-to-everyone-in-the-server/598207/2

Ad

Answer this question