The gun system should have whenever the gun is present in-hand of the player, it should then be able to play the sounds upon firing after its cooldowns.
Goal
The player can only have the script run if it is in their hand. Everything works and ends well with the cooldown too, but that is until it comes for the sound to be played rbxassetid://131070686
. It is a valid asset ID as it worked for whenever the player clicked (with and without the pistol).
Issue
Whenever it comes for the sound - gun shot sound effect (rbxassetid://131070686
) - to be played, it won’t play the sound and it breaks my code.
Setup
I have the server script for damaging players.
That script is where the cooldown and sound resides.
Code (server)
This is the action script.
It only breaks whenever I add the if statement for tool.
local tool = game.Players.LocalPlayer.Character:FindFirstChild("Pistol")
local cooldown = false
script.Parent.Fired.OnServerEvent:Connect(function(player, target)
if tool then
if cooldown == false then
if target:IsDescendantOf(player.Character) then
print("No damage")
else
if target.Parent:FindFirstChild("Humanoid") then
if target.Name == "Head" then
target.Parent.Humanoid.Health -= 20
print(tostring(target.Parent).." lost 20 health points.")
else
target.Parent.Humanoid.Health -=10
print(tostring(target.Parent).." lost 10 health points.")
end
end
script.gunShot:Play()
cooldown = true -- enables cooldown
wait(.5) -- cooldown is 5 seconds
cooldown = false -- disables cooldown
end
end
end
end)
Code (local)
This is the mouse-target script.
There is no issue here.
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
script.Parent.Fired:FireServer(mouse.Target)
end)
Let me know if I have missed something and feel free to link any external links in assistance to solving the issue.