Trying to add sound FX for a pepperspray system

I have a pepperspray system, client fires an event, server receives it and sprays the pepperspray. The client is activated by the player clicking on the pepper spray(as a tool).
So, I want to add audio, one for when the spray is spraying. Which I’m having trouble doing.
I also want to send audio, only for the local player who gets sprayed.(like heavy breathing or something)
Any assistance on how to do this would be appreciated.

Serverscript:

local function verify(plr)
	local t = false
	if plr.TeamColor == BrickColor.new("Lapis") then
		t = "Correctional Officers"
	end
	
	if plr.TeamColor == BrickColor.new("Dark blue") then
		t = "Team"
	end
	if not t then return false end
	return t
end

game.ReplicatedStorage.PepperEvents.Fire.OnServerEvent:connect(function(plr, s)
	if not verify(plr) then return end
	if s.Name ~= "Spray" then return end
	s.Enabled = not s.Enabled
	if s.Enabled then
	end
end)

Client:




local tool = script.Parent 
local pepperEventsFire = game.ReplicatedStorage.PepperEvents.Fire


tool.Activated:Connect(function() 

	pepperEventsFire:FireServer(tool.Fire.Spray) 
end)


tool.Unequipped:Connect(function() 

	if tool.Fire.Spray.Enabled then 

		pepperEventsFire:FireServer(tool.Fire.Spray) 
	end 
end)

Hitbox script(For the player who was hit, this is a serverscript and not local. This just shows a “orange” screen for the GUI effect as being blinded):

script.Parent.Touched:connect(function(hit)
	if not script.Parent.Parent.Fire.Spray.Enabled then return end
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		plr.Character.Humanoid.Sit = true
		if plr.PlayerGui:FindFirstChild("Peppered") then plr.PlayerGui:FindFirstChild("Peppered"):Destroy() end
		game.ReplicatedStorage.PepperEvents.Peppered:Clone().Parent = plr.PlayerGui
	end
end)

Play your pepper spray sound under the server script inside of OnServerEvent, and play your heavy breathing sound under the Activated event. To check for spray being sprayed, FireServer once the tool is Activated and FireServer again when the Tool is deactivated or unequipped. Create a sound with the looped property enabled and play that sound. Then stop the sound when the person stops spraying.

Okay, so the serverscript spraying works, thank you!
The breathing part is a little confusing, can you guide me a bit more please?

Client Sound Effects

local sound = <sound>

function toolDeactivate()
    -- Tool Deactivation Logic

    sound:Stop()
end

<tool>.Activated:Connect(function() 
    --- Tool Logic
    
    sound:Play()
end)

<tool>.Deactivated:Connect(toolDeactivate) 
<tool>.Unequipped:Connect(toolDeactivate) 

Okay, this helps a bit more. But where would I put this script?
In the Hitbox script? In the client? In the serverscript?
I’d assume the hitbox script, but that’s a serverscript so that couldn’t work…

Hi,

Will the sounds still stop it the player drops the tool via backspace etc…
also when they die or leave or reset?

1 Like

The code I provided is for the client.

For the server, you can do a similar thing;
Server Code

local PepperSprayEvent = <RemoteEvent>
local sound = <SpraySound>

PepperSprayEvent.OnServerEvent:Connect(function(plr, spray)
    if spray then
        sound:Play()
    else
        if sound.IsPlaying then
            sound:Stop()
        end
end)