Gunshot Shot SFX

  1. What do you want to achieve? I want to add gunshot SFX to my guns.

  2. What is the issue? Everytime I fire a gun, no audio plays.

  3. What solutions have you thought of so far? I tried to watch a YouTube video but it didn’t seem to work.

I’m a complete amateur, if possible please keep it simple.

1 Like

This thread needs significantly more details, please provide them - code, errors, issues you’ve identified and so on. Additionally, please do make sure you reference the Developer Hub for articles and API so you at least have some baseline knowledge on how to perform certain actions on the platform, such as playing sounds (see: Sound).

Server script:
script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*300,raycastParams)

if raycastResult then
	local hitPart = raycastResult.Instance
	local model = hitPart:FindFirstAncestorOfClass("Model")

	if model then
		if model:FindFirstChild("Humanoid")then
			model.Humanoid.Health -=50
		end
	end
end

end)

Client script:
local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()

script.Parent.Fire:FireServer(mouse.Hit.p)

end)

Does any of that code even mention a sound? If you never call :Play() on a sound, it will never play.

Am I supposed to add that into the server script? I tried to do that but it just makes the gun stop working.

You probably have to create a Sound object inside the Tool it you want it to play properly

You can either do it on the client (LocalScript) or the server (ServerScript), it shouldn’t break the gun if you just play a simple sound?

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - script.Parent.Handle.Position)*300,raycastParams)

if raycastResult then
	local hitPart = raycastResult.Instance
	local model = hitPart:FindFirstAncestorOfClass("Model")

	if model then
		if model:FindFirstChild("Humanoid")then
			model.Humanoid.Health -=20
		end
	end
end

end)

I want to add it to the server script, where should the line of code go?

Depends on where your Sound Object exactly is in the Tool, without it I can’t help you much apart from referencing the Character’s Model to obtain it

image_2021-05-26_163209

Is it supposed to look like this?

Place the sound in a part or an attachment. That wil give it the effect off emanating from that part.
More info you can read here Sound | Roblox Creator Documentation

1 Like

Yep

So you could just simply reference the Sound as player.Character.Tool.Sound, and play it when the OnServerEvent detects the event being fired

script.Parent.Fire.OnServerEvent:Connect(function(player,mousePos)

    player.Character.Tool.Sound:Play()

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {player.Character}
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    local raycastResult = workspace:Raycast(script.Parent.Handle.Position,(mousePos - 
    script.Parent.Handle.Position)*300,raycastParams)

    if raycastResult then
	    local hitPart = raycastResult.Instance
	    local model = hitPart:FindFirstAncestorOfClass("Model")

	    if model then
		    if model:FindFirstChild("Humanoid")then
			    model.Humanoid.Health -=20
            end
		end
	end
end)

If you wanted the play the Sound at a certain distance, you’d need to parent it inside a BasePart of some sort

1 Like

If you want the gun to make a sound you first have to make a sound instance

Alright, there we go. Now it works just fine. Thanks.