PlayLocalSound not working when onTouched event is triggered

Here’s the error I get in the output: SoundService:PlayLocalSound only works on a client. Does anyone know a solution to this?

Script located in ServerScriptService:

 local SoundService = game:GetService("SoundService")

function onTouch(part)
	part.Parent:MoveTo(game.Workspace.ToS2.Position)
	wait(0.5)

	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://143562367"
	
	SoundService:PlayLocalSound(sound)
end

game.Workspace.Portals.Portal1.ToS1.Touched:Connect(onTouch)

The error is self explanatory. Move it to a local script and it will work.

1 Like

nope, tried that 30 charsssssss

You need a local script to execute this function. Local scripts do not work in ServerScriptService.

The only way you can have any sound played locally is to have the physical sound be played on the client, no question about it. I think you would be better off to just use :Play() on the sound or have a local script play the sound when you want to and then handle everything else on the server.

The sound needs to be parented somewhere so it can emit sound depending on its parent. For instance in workspace it will play globally. But on a part it will emit sound from that part.

local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://143562367"
sound.Parent = workspace

local debounce = false

local function onTouch(part)
	if debounce then
		return
	end
	
	debounce = true
	sound:Play()
	sound.Ended:Wait()
	debounce = false
end

game.Workspace.Portals.Portal1.ToS1.Touched:Connect(onTouch)

(as a localscript)

2 Likes

still won’t work, unfortunately.

How does it not work? The only reason it wouldn’t work is if you changed something up.

Here is a place that demonstrates that it in fact works: it_does_work.rbxl (18,5 KB)

1 Like

forgot to put it in StarterPlayerScripts, sorry ab that.