How to play a local sound if a player touches a part

local TouchDec = script.Parent
local ScreenText = TouchDec.Parent.SpotScreen.SurfaceGui.ScreenFrame.ScreenText
local PlayersTouching = {}
TouchDec.Touched:Connect(function(Touch)
	local player = game:GetService("Players"):GetPlayerFromCharacter(Touch.Parent) 
	if player and not table.find(PlayersTouching, player.UserId) then
		table.insert(PlayersTouching, player.UserId)
		ScreenText.Text = #PlayersTouching .. "/2"
		print(#PlayersTouching)
		game.SoundService:PlayLocalSound(game.SoundService.SpotSound)
	end
end)
TouchDec.TouchEnded:Connect(function(TouchEnd)
	local player = game:GetService("Players"):GetPlayerFromCharacter(TouchEnd.Parent) 
	if player and table.find(PlayersTouching, player.UserId) then
		table.remove(PlayersTouching, table.find(PlayersTouching, player.UserId))
		ScreenText.Text = #PlayersTouching .. "/2" 
		print(#PlayersTouching)
	end
end)

so I have this script that changes the spotscreen text depending on how many players are touching the TouchDec (Invisble Part) and I want to play a sound when a player touches the part I tried using PlayLocalSound as you see here

game.SoundService:PlayLocalSound(game.SoundService.SpotSound)

but I always get this error on the output

SoundService:PlayLocalSound only works on a client.

so basically I want to play the sound for the player who touched the TouchDec only (Locally)

1 Like

You are firing that function in a server script. Either run it in a local script or use a remote event.

Copy the code but put it in a normal script, the go to the scripts property and set the run context to client.

Normally local scripts only run in the player, player character and replicated first, but if you change a scripts run context to client it will work like a normal script but will run in normal scripts descendants too.

Also make sure SoundService.RespectFilteringEnabled is on otherwise the sound will still play on the server

Thank you that works and sorry for the last btw the script is not a local script already lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.