Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

why is SoundService:PlayLocalSound not playing sound locally?

Asked by 3 years ago

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))

2 answers

Log in to vote
0
Answered by
sayer80 457 Moderation Voter
3 years ago
Edited 3 years ago

I could think of 1 Error: If you're testing in Studio then its possible to hear the game sounds of other clients while testing. Also :Play() just does fine when your doing it in a localscript

You have to check who is actually touching the coin

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
if player.Parent:FindFirstChild("Humanoid") ~= nil and player.Parent.Name == game.Players.LocalPlayer.Name then


        wait()
        debounce = false
        coinCollection:FireServer()
        SoundService:Play(coinSound)
        wait(10)
        debounce = true
    end
end
end)
0
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. YT_GamingDan 10 — 3y
0
OH i found the mistake sayer80 457 — 3y
1
Thanks, it works fine now. YT_GamingDan 10 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

Don't use PlayLocalSound().

Trying to use PlayLocalSound() is about as redundant as re-inventing GetChildren() solely to recursively return children (which GetDescendants() does). I'm not certain why you aren't going to the simple solution of playing the sound on the client using Play() from a LocalScript. Doing this has the same intended functionality as PlayLocalSound(), but what is the entire point? PlayLocalSound() actually plays the sound on every client because it's run on the server.

That being said, you should use a RemoteEvent or RemoteFunction to allow the coin sound to play on the client while handling anything that should be handled on the server.

0
I used changed the local script to use coinSound:Play(), it still plays the sound to other players once a player collects the coin YT_GamingDan 10 — 3y

Answer this question