✓ Accepted answer
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
3 comments
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
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?
Sound is good. Fine in Explorer, studio and my other project (sutdio and server). Not sausage on the error front either.