It looks like you're using remote events, but what you've provided is only the part where the server fires the client. What's not provided is how the client responds to that fire, but I would assume that a LocalScript in your game :Play()s the audio.
My best guess is that you're having the client play the audio so only they hear it and none of the other players, and that you're running into issues because the sound is playing repeatedly as they touch the part?
I tried my best to replicate what I think you're doing -
In a part in the workspace:
local sound = "rbxassetid://138088421"
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.PlayMusic:FireClient(player,sound)
end
end)
In a local script in the player:
function playMusic(id)
workspace.Sound.SoundId = id
workspace.Sound:Play();
end
game:GetService("ReplicatedStorage").PlayMusic.OnClientEvent:Connect(playMusic)
This seemed to work, and I was able to replicate your problem (the sound effect playing multiple times)
Since you're having the server tell the client when to play, I can understand that you probably wouldn't want the part to only play it once, since other players wouldn't be able to hear it. I'd recommend just letting the part remember who has and has not heard the effect.
Solution
In the part:
local heard = {}
local sound = "rbxassetid://138088421"
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local alreadyHeard = false
for i,v in pairs(heard) do
if hit.Parent.Name == v then
alreadyHeard = true
end
end
if not alreadyHeard then
table.insert(heard,#heard+1,hit.Parent.Name)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
game.ReplicatedStorage.PlayMusic:FireClient(player,sound)
end
end
end)
I hope this helped! If it did, please mark it as the solution or upvote it. If you have any questions, please comment below :)
1 comment
Thanks so much!