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.

01db = false
02script.Parent.Touched:connect(function(hit)
03    if hit.Parent:FindFirstChild('Humanoid') then
04        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
05        if db == false then
06            db = true
07            script.Parent.Transparency = 1
08            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
09            player.Backpack:WaitForChild('Kaching')
10            player.Backpack.Kaching:Play()
11            wait(math.random(30,60))
12            script.Parent.Transparency = 0
13            db = false
14        else
15        end
16    end
17end)
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

01local players = game:GetService("Players");
02local player = players.LocalPlayer;
03local rs = game:GetService("ReplicatedStorage");
04local parent_sound_ev = rs:WaitForChild("Parent Sound");
05local cam = workspace.CurrentCamera;
06 
07function parent_it()
08    local sound = script:WaitForChild("Kaching"):Clone();
09    local prev_sound = cam:FindFirstChildOfClass("Sound");
10 
11    if prev_sound then
12        prev_sound:Destroy();
13    end
14 
15    sound.Parent = cam;
16    sound:Play();
17end
18 
19parent_sound_ev.OnClientEvent:Connect(parent_it);

Server Script

01db = false
02local rs = game:GetService("ReplicatedStorage");
03local play_sound = rs:WaitForChild("Parent Sound")
04 
05script.Parent.Touched:connect(function(hit)
06    if hit.Parent:FindFirstChild('Humanoid') then
07        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
08        if db == false then
09            db = true
10            script.Parent.Transparency = 1
11            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 1
12            play_sound:FireClient(player);
13 
14            wait(math.random())
15            script.Parent.Transparency = 0
16            db = false
17        else
18        end
19    end
20end)

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