Sound is playing for everyone in the game for some reason!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

A working coin/Gems sound system

  1. What is the issue?

The issue is that the sound plays but like everyone in the server could hear.

  1. What solutions have you tried so far?

Finding a solution

It is a local script!

local Gems = game.Workspace.Gems

local player = game.Players.LocalPlayer

local db = true
local sound = script.Parent.Sound
for i, v in pairs(Gems:GetChildren()) do
	v.Touched:Connect(function()
		if v.Transparency == 0 then
			if db == true then
				db = false
				sound:Play()
				wait(0.15)
				db = true
			end
		end
	end)
end

Thank you in advance!

This touched function runs for everyone in the server. Check to make sure if the touched player is the same as the local player.

local Gems = game.Workspace.Gems

local player = game.Players.LocalPlayer

local db = true
local sound = script.Parent.Sound
for i, v in pairs(Gems:GetChildren()) do
	v.Touched:Connect(function(hit)
		if game.Players:GetPlayerFromCharacter(hit.Parent) == game.Players.LocalPlayer then
			if v.Transparency == 0 then
				if db == true then
					db = false
					sound:Play()
					wait(0.15)
					db = true
				end
			end
		end
	end)
end

Yeah that’s because this is a LocalScript with no checks on who touched the coin. Other players touching a coin will still be registered as a touch on the current client so the sound is going to play. The main concern here is that the touch is being observed by all clients.

I wrote a thread on this:

1 Like

Inside the .Touched event you aren’t checking if the player touching the coin is equal to LocalPlayer which makes it fire for every player inside their own LocalScript. Instead add an if statement to check if the LocalPlayer is the one touching the coin:

local Players = game:GetService("Players")

local Gems = workspace.Gems
local player = Players.LocalPlayer

local db = true
local sound = script.Parent.Sound
for i, v in pairs(Gems:GetChildren()) do
	v.Touched:Connect(function(hit)
		if player ~= Players:GetPlayerFromCharacter(hit.Parent) then return end 
		if v.Transparency == 0 then
			if db == true then
				db = false
				sound:Play()
				wait(0.15)
				db = true
			end
		end
	end)
end

Thank you everyone who helped, this makes more sense now!