How do I play a sound locally?

I don’t know how to make it so if the local player touches a part, only they can hear it, but know one else can hear it unless the touch the same part. Do you think this code would work?

local Part = script.Parent
local Sound = Part.Sound
Part.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild:("Humanoid") then
Sound:Play()
end

If I were to make the script a local script, would it work?

2 Likes

That would work, but not in the way you’d like it to, it will make the touched:connect fire for every player.

so if another player touches the part, all players still hear it, but locally.

2 Likes

If you want the local player to hear a sound after a part is touched it would look something like this

--Inside a local script

local PartToTouch = workspace.Part
local debounce = false
local Sound = game.ReplicatedStorage.Sound

PartToTouch.Touched:Connect(function(hit)
	if hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
		if debounce then return end
		debounce = true
		Sound:Play()
		wait(Sound.TimeLength)
		debounce = false
	end
end)
2 Likes

the best solution would involve using a remote event, but the easiest is just to do a playercheck in the localscript

local Part = somewhere.Part
local Sound = Part.Sound

Part.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") == nil then return end
	if game.Players:GetPlayerFromCharacter(Hit.Parent) ~= game.Players.LocalPlayer then
		return
	end
	
	Sound:Play()
end)
1 Like

I will test your code tomarrow!

Thank you so much man! It worked!

Wait a minute:

Sound.Ended:Wait()

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