Ad
Log in to vote
0

Having an issue with the sounds of a gun, does anybody know how to fix this?

Asked by 23 days ago

I'm currently working on a game, and the gun in the game works fine, except the sounds only play for the character shooting for the gun. The script is currently a LocalScript, does anybody know how to fix this?

Here is the script:

local gun = script.Parent
local gun_sound = gun.Handle['Gun shot']
local empty_sound = gun.Handle.clip_empty
local reload_sound = gun.Handle.Reload
local player = game.Players.LocalPlayer
local clipSize = gun:WaitForChild('Ammo').Value
local ammo = gun:WaitForChild('Ammo')




--UserInputService Setup
local userInput = game:GetService('UserInputService')


--Mouse Icon
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Icon = 'rbxassetid://316279304'


local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')

gun.Equipped:Connect(function(mouse)

    player.PlayerGui.ScreenGui.Ammo.Visible = true
    player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo:' .. tostring(ammo.Value) ..' / ' .. tostring(gun.MaxAmmo.Value)


    gun.Unequipped:Connect(function()
        player.PlayerGui.ScreenGui.Ammo.Visible = false
    end)


    mouse.Button1Down:Connect(function()
        if gun.Ammo.Value > 0 then
        remoteEvent:FireServer(gun.Handle.Position, mouse.Hit.p)
            gun_sound:Play()
            gun.Ammo.Value = gun.Ammo.Value - 1
        else
            reload_sound:Play()
            reload_sound.Ended:Wait()
            gun.Ammo.Value = 12
        end
    end)

end)


--Checks if the Letter R is pressed

userInput.InputBegan:Connect(function(input, gameProcessed)

    if not gameProcessed then
        if input.UserInputType == Enum.UserInputType.Keyboard or input.UserInputType == Enum.UserInputType.Gamepad1 then
            local keycode = input.KeyCode
            if keycode == Enum.KeyCode.R or keycode == Enum.KeyCode.ButtonX then
                reload_sound:Play()
                reload_sound.Ended:Wait()
                gun.Ammo.Value = 12
            end

        end
    end
end)

--Update ammo GUI

ammo.Changed:Connect(function()
    player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo:' .. tostring(ammo.Value) ..' / ' .. tostring(gun.MaxAmmo.Value)
end)

2 answers

Log in to vote
0
Answered by 23 days ago

The sound should not be called from a localscript, only the player that shoots the gun would hear it.

Ad
Log in to vote
0
Answered by 23 days ago

Anything called from a LocalScript will only apply on the client that triggered the call. Hence why only the client that calls the script hears the noise. It should be fixable by calling it through a regular script.

0
I tried putting the gun script into a regular script, but the gun stopped working entirely. NuggetHasJoined 7 — 21d

Answer this question