Hello Developers!
I was recently working on a game which is focused on fighting and PvP using guns. I have created all the guns, except for the fact that all of them all are Semi-Automatic (Click to shoot), and I just created an automatic script for them. The problem with the script is that it doesn’t play the fire sound every time it is fired.
Forgot to mention: The gun does work fully automatically, the only problem is the firing sound, it only plays in the beginning of the mousebuttondown and end of mousebuttonup
Here’s the full script:
local tool = script.Parent
local WeaponTool = tool:WaitForChild("Gun")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
local idle = "rbxassetid://9212173767"
local reload = "rbxassetid://9212239484"
local fire = "rbxassetid://9212167959"
local out = "rbxassetid://9212173767"
local inspect = "rbxassetid://9212173767"
local plr = game.Players.LocalPlayer
local data = game:GetService("ReplicatedStorage").Remote.Data
local ammo = script.Parent.Ammo
local leftammo = script.Parent.Left
local Sfire = script.Parent:WaitForChild("Sounds / Config").Fire
local SReload = script.Parent:WaitForChild("Sounds / Config").reload
SReload.PlaybackSpeed = 1
local lolboom = game:GetService("StarterGui")
local reloading = false
local inspecting = false
local down = false -- used for automatic
local firerate = 1 -- delay
local function reloadgun()
if not reloading and leftammo.Value >= 1 then
reloading = true
local char = script.Parent.Parent
local anim = Instance.new("Animation", char)
anim.AnimationId = reload
local human = char:FindFirstChild("Humanoid")
local valid = human:LoadAnimation(anim)
valid.Priority = Enum.AnimationPriority.Action4
valid:Play()
SReload:Play()
ammo.Value = script.Parent.Maxammo.Value
leftammo.Value -= script.Parent.Maxammo.Value
valid.Stopped:Connect(function()
data:FireServer(ammo.Value, leftammo.Value, tool.Name)
reloading = false
end)
end
end
local function inspectgun()
if not reloading and inspecting then
inspecting = true
local char = script.Parent.Parent
local anim = Instance.new("Animation", char)
anim.AnimationId = inspect
local human = char:FindFirstChild("Humanoid")
local valid = human:LoadAnimation(anim)
valid.Priority = Enum.AnimationPriority.Action4
valid:Play()
valid.Stopped:Connect(function()
inspecting = false
end)
end
end
script.Parent.Equipped:Connect(function()
local char = script.Parent.Parent
game.ReplicatedStorage.Remote.ConnectM6D:FireServer(WeaponTool.BodyHand)
char["Right Arm"].ToolGrip.Part0 = char["Right Arm"]
char["Right Arm"].ToolGrip.Part1 = WeaponTool.BodyHand
local anim = Instance.new("Animation", char)
anim.AnimationId = idle
local human = char:FindFirstChild("Humanoid")
local valid = human:LoadAnimation(anim)
valid.Priority = Enum.AnimationPriority.Action4
valid:Play()
tool.Unequipped:Connect(function()
game.ReplicatedStorage.Remote.DisconnectM6D:FireServer()
valid:Stop()
end)
data:FireServer(ammo.Value, leftammo.Value, script.Parent.Maxammo.Value, tool.Name)
end)
tool.Unequipped:Connect(function()
game.ReplicatedStorage.Remote.DisconnectM6D:FireServer()
end)
RS.RenderStepped:Connect(function()
while not reloading and not inspecting and ammo.Value >= 1 and down do
local char = script.Parent.Parent
local anim = Instance.new("Animation", char)
anim.AnimationId = fire
local human = char:FindFirstChild("Humanoid")
local valid = human:LoadAnimation(anim)
valid.Priority = Enum.AnimationPriority.Action4
valid:Play()
local pos = mouse.Hit.Position
Sfire:Play()
ammo.Value -= 1
tool.Funni:FireServer(pos)
data:FireServer(ammo.Value, leftammo.Value, script.Parent.Maxammo.Value, tool.Name)
task.wait(firerate)
valid.Stopped:Wait()
end
end)
UIS.InputBegan:Connect(function(input, GPE)
if input.UserInputType == Enum.UserInputType.Keyboard then
if input.KeyCode == Enum.KeyCode.R and not plr.Backpack:FindFirstChild("Blocky Carbine") then
reloadgun()
elseif input.KeyCode == Enum.KeyCode.F and not plr.Backpack:FindFirstChild("Blocky Carbine") then
print("INSPECTING")
if not reloading and inspecting then
inspecting = true
local char = script.Parent.Parent
local anim = Instance.new("Animation", char)
anim.AnimationId = inspect
local human = char:FindFirstChild("Humanoid")
local valid = human:LoadAnimation(anim)
valid.Priority = Enum.AnimationPriority.Action
valid:Play()
valid.Stopped:Connect(function()
inspecting = false
end)
end
end
elseif input.UserInputType == Enum.UserInputType.MouseButton1 and not plr.Backpack:FindFirstChild("Blocky Carbine") then
down = true
end
end)
UIS.InputEnded:Connect(function(input, gpe)
if input.UserInputType == Enum.UserInputType.MouseButton1 and not plr.Backpack:FindFirstChild("Blocky Carbine") then
down = false
end
end)
Any Help is Appreciated,
OceanTubez