Audio being heard globally and sound not destroying

When a player holds Q a sound starts to play like it should but the sound never gets destroyed and plays for way longer than intended and when someone spams the q button the sound stacks and makes it really loud. On top of that the sound can be heard globally.

local rs = game.ReplicatedStorage
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://7329829970"
animation.Name = "CoolAnim"
local pressed = false

rs.Charging.OnServerEvent:Connect(function(player, value)
	local S = Instance.new("Sound")
	S.SoundId = "rbxassetid://1085614185"
	S.PlaybackSpeed = math.random(80,120)/100
	S.Volume = math.random(30,60)/100
	S.RollOffMinDistance = 50
	S.RollOffMaxDistance = 60
	S.Name = "ChargeAudio"
	S.Parent = player.Character
	S:Play()
	
	pressed = true
	if player.Skills:FindFirstChild("ManaChargeI") ~= nil then
	local char = player.Character
	local track = char:WaitForChild("Humanoid"):LoadAnimation(animation)
		if value == "IsCharging" then
			track:Play()
			local FX1 = game.ReplicatedStorage.ManaCharge:clone()
			FX1.CFrame = char.HumanoidRootPart.CFrame
			FX1.Parent = char
			game.Debris:AddItem(FX1,9999999)
			char.Humanoid.WalkSpeed = 0
			char.Humanoid.JumpPower = 0
			S:Play()
			while pressed == true and char.Humanoid.Mana.Value < char.Humanoid.MaxMana.Value do
				wait(.1)
				FX1.CFrame = char.HumanoidRootPart.CFrame
			char.Humanoid.Mana.Value += 1
			end
			
		elseif value == "IsNotCharging" then
			S:Stop()
			S:Destroy()
			char.Humanoid.WalkSpeed = 16
			char.Humanoid.JumpPower = 50
			pressed = false
			char:FindFirstChild("ManaCharge"):Destroy()
		
			for _, track in ipairs(char.Humanoid:GetPlayingAnimationTracks()) do
				if track.Name == "CoolAnim" then
					track:Stop()
				
					break
				end
			end
		end
	end
end)

It’s because you’re playing it on the server. So everyone can hear it.

Ive had other scripts where its on a server script yet plays for only the one person what did i do wrong on this script?

To fix the spamming issue just create a cool down by having a value which is false at the start and then make it true when the audio is played put a wait for how long you want it and then make it false again. (u can look if it is true or false the cool down by a if statment).

The reason why the audio can be heard by all is due to like what @RensUnnecessaryAlt said about this being a server script. You would need to run the code which activates the audio in a local script so just the local player can hear it.

Also you should be able to make the audio property playing to false when you want it off and true when you want it to play,

That should not really be possible due to the server being what everyone can see/hear and the local scripts being used for locally things.

What? How would that even work?
Anyways, if you want only the player to be able to hear the sound, you can just play it on the client itself. Also, do what @LifeDigger said, add a cooldown.

Alright Ill try changing it now

Wait. I think I get what you’re saying. Was the sound inside a part?
Pretty sure that if a sound is inside a physical object it won’t play for other users if they’re far away from it.

1 Like

Yes it was inside of a sword model, but i get your point now that if its not in a part then it will be heard globally (i think)

I changed a lot of stuff around from server to client and it seemed to work thanks very much.