I’m trying to play a sound after you collect the coin (scrap coin) and then play a local sound only to the player who interacted with it
The thing is everyone in the server can hear it. The sound is not local. I don’t want it to be a LocalScript. I already tried Maxdistance but doesnt seem to work. I tried using Remote events aswell.
Anyone know how to fix this? This is the script unmodified.
Well why don’t you want to use a localscript?
Just use :FireClient on your server sided script and have a localscript listen to it. and play the sound locally. Is there a particular reason you want to avoid that?
I no longer have my remote event script. But all it did was activated the remote event, then it plays the sound. The thing was that the sound wasnt playing at all anymore and the coin would be uncollectable. So I gave up on that. I might have done it wrong tho.
When I meant I don’t want a localscript, I simply meant that I wanted the coin to be collected by who ever found it first in the server. if it was localscript, then all the players will be able to collect the coin without competition. But using 1 Local and 1 server script to play the sound sounds like a good idea. I’ll try with my novice scripting skills but any examples will help.
If you have some freetime just take a seat and read this carefully.
Try to replicate it with your own little touch, if it doesn’t work and you run into an issue look for answers all over the internet or look at the documentation again.
If you run into something very specific and need help with it, you can come back here though.
Yeah ill see if I will have the time to read. So far I have this. It gives the the currency and disappears, but it wont play the sound at all. The local script is in StarterPlayer > StarterCharacterScript. Remote event is in ReplicatedStorage.
Curious what the coin’s name is? Because you can easily just do this.
function CharacterHit(Hit)
if tostring(Hit):match("Coin") then
if not Hit:GetAttribute("OnCD") then
game:GetService("SoundService"):PlayLocalSound(Hit.Sound)
end
end
end
My first photo shows the name, structure, and script. I think i got it now becuase i put a local script in a screen gui but im going to test it. if it doesnt then ill try that instead.
But first to utilize that function you need to use this. First script belongs in startercharacter as a client.
local Character:Model? = script.Parent
function CharacterHit(Hit)
if tostring(Hit):match("Coin") then
if not Hit:GetAttribute("OnCD") then
game:GetService("SoundService"):PlayLocalSound(Hit.Sound)
end
end
end
for _,v:BasePart? in Character:GetChildren() do
if v:IsA("BasePart") then
v.Touched:Connect(function(otherPart: BasePart)
CharacterHit(otherPart)
end)
end
end
and instead of generic local db use this as a cooldown.
The code seems fine, although you need to have the localscript be a descendant of either the player (such as PlayerGui) or of the player’s character, otherwise it won’t run.
You’ll need to change the location of sound after making that change to make sure it gets the correct sound.
Then, you can use the PlayLocalSound() function of SoundService to play it.
i.e.
local sound = ...
game.ReplicatedStorage.PlaySound.OnClientEvent:Connect(function()
game:GetService("SoundService"):PlayLocalSound(sound)
end)
I was able to fix it, as I stated earlier, I simple put a ScreenGUI in StarterGUI, Put the local script and the sound in it. Put the remote event aswell in replicate storage, and the main script in the interactable object. Thanks everyone for your tips and support.