Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sound is not playing for the server whenever the player shoots their weapon. Any fixes or tips?

Asked by 4 years ago

So I've been making a gun framework which I would be able to apply to all of my future projects, but I've stumbled across a problem where whenever the player shoots their weapon, it only plays locally and not publicly. So far I've tried turning off 'RespectFilteringEnabled' in the Sound Service and writing a script that would play it publicly, but none of these methods have worked.

local debounce = false

local cooldownTime = 0.4

script.Parent.Activated:Connect(function()
    if not debounce and Ammo > 0 then
        debounce = true
        local shooting = game:GetService("Players").LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Shoot)

        local sound = game:GetService("StarterPack").Pistol.Sound

        sound:Play()
        shooting:Play()
        Ammo = Ammo - 1
        wait(cooldownTime)
        debounce = false
    end
end)

Any feedback would be appreciated!

0
Well Is This A Local Script? crueluu 169 — 4y
0
Well yes. I forgot to mention that. littlekit07 16 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

You can just simply fire a RemoteEvent whenever a player shoot's a gun. Insert a RemoteEvent in ReplicatedStorage and insert a script inside ServerScriptService as well. This will play the sound globally so everyone hears it!

Local script:

local Players = game:GetService("Players")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local StarterPack = game:GetService("StarterPack")

local debounce = false

local cooldownTime = 0.4

script.Parent.Activated:Connect(function()
    if not debounce and Ammo > 0 then
        debounce = true
        local shooting = Players.LocalPlayer.Character:WaitForChild("Humanoid"):LoadAnimation(script.Parent.Shoot)

        local sound = StarterPack:WaitForChild("Pistol").Sound

        ReplicatedStorage:WaitForChild("RemoteEvent"):FireServer()
        shooting:Play()
        Ammo = Ammo - 1
        wait(cooldownTime)
        debounce = false
    end
end)

Server script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local StarterPack = game:GetService("StarterPack")

local sound = StarterPack:WaitForChild("Pistol").Sound


ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
        sound:Play()
end)
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

Find where the event happens, right before it ends write:~~~~~~~~~~~~~~~~~game.Workspace.Sound:Play()

~~~~~~~~~~~~~~~~~ Replace workspace and sound with there names

Answer this question