How to make sound only play for players that are close to the source?

the script i have currently is supposed too play an explosion sound when a player types the word explode, and only players within 50 studs of the person who typed the message should hear it. The problem is that only the person who typed the message hears the sound. Here’s the scripts

--Server Script
game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
		if msg == "explode" then
			game.ReplicatedStorage.soundplay:FireAllClients(plr.Character.HumanoidRootPart.Position, workspace.explosion)
		end
	end)
end)

--Local Script
game.ReplicatedStorage.soundplay.OnClientEvent:Connect(function(pos, sound)
	if (pos - hrp.Position).Magnitude < 50 then
		sound:Play()
	end
end)

Put the sound (sound.Parent) inside of a part like a humanoidRootPart so it will play from it. But you will probably have to tweak the volume a bit

1 Like

Parent the sound part to a specific part. And change the Sound.RollOffMaxDistance (in studs) to how far you can hear it. in this case you want to set it to 50.

2 Likes

You need to combine what both @ProKrzak_PL and @rgonhplib46359 said.

A Sound will play in 3 places:
Workplace sounds play everywhere.
In a Part or an Attachment.

In your case use the HumanoidRootPart for the sound.
You also will probably need to adjust the Volume, RollOffMinDistance and RollOffMaxDistance to get the effect you want.
Also change the RollOffMode. I prefer InverseTapered since it gives max volume at your RollOffMinDistance, and tapers off gradually to 0 when at your RollOffMaxDistance.
The default setting of Inverse tapers off but has a sudden cutoff at the RollOffMaxDistance.

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