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

? Local Sound not playing

Asked by 6 years ago

Learning as i go from my mistakes, go easy on me.

This worked in another project so i copied it to a new one, now i cannot hear the sound outside of studio(Server).

Game is Non-FE and i cannot find the difference/Error between my two projects.

db = false
script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if db == false then
            db = true
            script.Parent.Transparency = 1
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
            player.Backpack:WaitForChild('Kaching')
            player.Backpack.Kaching:Play()
            wait(math.random(30,60))
            script.Parent.Transparency = 0
            db = false
        else
        end
    end
end)
0
Note that i also tried this by having the sound in storage and cloning/removing it to/from the backpack but had the same issue Yishles 4 — 6y
0
Might be an issue with the sound object. Have you tried playing the sound directly from the explorer? Or checking in your script to see if its correctly indexing the sound object? chomboghai 2044 — 6y
0
Sound is good. Fine in Explorer, studio and my other project (sutdio and server). Not sausage on the error front either. Yishles 4 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hi Yish,

I think the reason it's not playing in the game is because you placed it in Backpack. If you want a Sound to be local, as in heard only by one player, then you need to place it into the CurrentCamera or the Player itself. However, keep in mind that the CurrentCamera is client-side only. So, you must use a LocalScript to access it. Therefore, you need to fire a RemoteEvent from the Server to set off the LocalScript. Then, this LocalScript will parent the Sound and play it.

Here's how the Server and LocalScript look like:

Local Script

local players = game:GetService("Players");
local player = players.LocalPlayer;
local rs = game:GetService("ReplicatedStorage");
local parent_sound_ev = rs:WaitForChild("Parent Sound");
local cam = workspace.CurrentCamera;

function parent_it()
    local sound = script:WaitForChild("Kaching"):Clone();
    local prev_sound = cam:FindFirstChildOfClass("Sound");

    if prev_sound then
        prev_sound:Destroy();
    end

    sound.Parent = cam;
    sound:Play();
end

parent_sound_ev.OnClientEvent:Connect(parent_it);

Server Script

db = false
local rs = game:GetService("ReplicatedStorage");
local play_sound = rs:WaitForChild("Parent Sound")

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if db == false then
            db = true
            script.Parent.Transparency = 1
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
            play_sound:FireClient(player);

            wait(math.random())
            script.Parent.Transparency = 0
            db = false
        else
        end
    end
end)

I've made a place that does this, and I've allowed copying so you can take a copy and examine the whole system if you need a better handle on it. It can be found here.

Well, I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
Wow, what a reply. I'm yet to learn anything on remotes, still grasping the basics haha. Seems i was trying to play the sound In the player rather than from it. Thank you so much for the effort in your answer, just reading over the place you provided will help me learn much easier. Thank you once again ! Yishles 4 — 6y
0
No problem. I'm glad to have helped. KingLoneCat 2642 — 6y
Ad

Answer this question