Hello, recently I have been working on a Zombie Tower rework, and the gun system has been going very well, but there is just 1 issue. Holding left click is perfectly fine with an automatic gun, but when I spam left click the gun goes insane. Here is what happens:
Here is the code you may need:
UserInputService.InputBegan:Connect(function(InputObject, GameProcessedEvent)
if not GameProcessedEvent then
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
if not CanShoot then
return
end
if Shooting then
return
end
if ViewModel:FindFirstChild("AmmoValue", true).Value <= 0 then
return
end
if ViewModel:FindFirstChild("AmmoValue", true).Value <= 0 and ViewModel:FindFirstChild("ChamberValue", true).Value <= 0 then
return
end
local StartShoot = time()
Shooting = true
if GunValues[CurrentEquipped].IsAutomatic then
repeat
ViewModel:FindFirstChild("AmmoValue", true).Value -= 1
task.spawn(RenderBullet)
task.spawn(RecoilSetup)
print(CurrentEquipped, "Casted a Bullet")
local ShootAnimation = Animator:LoadAnimation(PresetShootAnimation)
ShootAnimation:Play()
task.wait(GunValues[CurrentEquipped].ShootCooldown)
until not Shooting or not CanShoot or ViewModel:FindFirstChild("AmmoValue", true).Value <= 0
else
ViewModel:FindFirstChild("AmmoValue", true).Value -= 1
task.spawn(RenderBullet)
task.spawn(RecoilSetup)
print(CurrentEquipped, "Casted a Bullet")
task.wait(GunValues[CurrentEquipped].ShootCooldown)
end
end
if InputObject.UserInputType == Enum.UserInputType.MouseButton2 then
if Reloading then
return
end
Aiming = true
end
if InputObject.UserInputType == Enum.UserInputType.Keyboard then
if InputObject.KeyCode ~= Enum.KeyCode.R then
return
end
if Shooting then
return
end
if Reloading then
return
end
if Aiming then
return
end
if ViewModel:FindFirstChild("AmmoValue", true).Value > 0 then
return
end
if ViewModel:FindFirstChild("ChamberValue", true).Value <= 0 then
return
end
Reloading = true
CanShoot = false
task.spawn(function()
task.wait(GunValues[CurrentEquipped].ReloadTime)
Reloading = false
CanShoot = true
ViewModel:FindFirstChild("AmmoValue", true).Value = GunValues[CurrentEquipped].AmmoValue
ViewModel:FindFirstChild("ChamberValue", true).Value -= GunValues[CurrentEquipped].AmmoValue
end)
--[[task.delay(GunValues[CurrentEquipped].ReloadTime, function()
Reloading = false
CanShoot = true
end)--]]
local ReloadAnimation = Animator:LoadAnimation(PresetReloadAnimation)
ReloadAnimation:Play()
local EquippedGun = ViewModel:FindFirstChild(CurrentEquipped)
if EquippedGun then
local MainHandle = EquippedGun:FindFirstChild("MainHandle", true)
if MainHandle then
task.spawn(function()
task.wait(.85)
local ReloadSound = MakeSound(GunValues[CurrentEquipped].ReloadSoundId, false, .51, 1.75, MainHandle)
ReloadSound:Play()
end)
end
end
end
end
end)
UserInputService.InputEnded:Connect(function(InputObject, GameProcessedEvent)
if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
if Reloading then
return
end
Shooting = false
end
if InputObject.UserInputType == Enum.UserInputType.MouseButton2 then
Aiming = false
end
end)
Any help is appreciated, thanks!