I am making a coin collection system, when the user runs over the coin I want a sound to play locally so only the player collecting the coin can hear that sound, however I am having some issues with this. When I use SoundService:PlayLocalSound() the sound is not played locally therefore everyone around the coin that was just collected can hear the sound. This is the script I am using to fire a RemoteEvent and also make the sound (local script):
local coin = game.Workspace.Coin
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coinCollection = ReplicatedStorage:WaitForChild("coinCollection")
local SoundService = game:GetService("SoundService")
local coinSound = Instance.new("Sound")
coinSound.SoundId = "rbxassetid://"..3302699870
local debounce = true
coin.Touched:Connect(function(player, hit)
if debounce == true then
wait()
debounce = false
coinCollection:FireServer()
SoundService:PlayLocalSound(coinSound)
wait(10)
debounce = true
end
end)
And if you need it, here is the server script for when the remote event is fired:
function debounce(func) -- debounce to run the coins event once
local isRunning = false -- debounce automatically set as not running
return function(hit) -- connecting coins event to stimulate start of debounce
if not isRunning then -- debounce starts as the coin event starts, it starts a decision
isRunning = true -- debounce starts
func(hit) -- it runs the coin event once
isRunning = false -- it ends the coin event if hit only 1 time
end
end
end
local Players = game:GetService("Players")
local coin = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local coinCollection = ReplicatedStorage:WaitForChild("coinCollection")
--Referancing the coin for the event to run the coins function
coinCollection.OnServerEvent:Connect(debounce(function(player, hit)
--making the part invisible
coin.Transparency = 1
--adding 1 to the players Coin Balance
local player = player
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
--Waiting 10 seconds untill making object visible
wait(10)
coin.Transparency = 0
end))

3 comments
I just tried testing this on a public Roblox game and other players can still hear the coin collection sound when a player has collected the coin.
OH i found the mistake
Thanks, it works fine now.