So here's the deal; I tried making an automatic gun but failed. If I hold my mouse button it will work how I intended, but as soon as I spam the gun it shoots out the bullets extremely fast. The ammo counter will also decrease dramatically. Is there any way fix to this? Any answer is appreciated!
Most of the script (You might have to press view source):
script.Parent.Equipped:Connect(function(mouse)
canReload = true
ammoGui.Text = ammo.." / "..maxAmmo
canIdle = true
script.Parent.Unequipped:Connect(function()
canIdle = false
canReload = false
canShoot = false
shooting = false
ammoGui.Text = "---"
equipAnim:Stop()
idleAnim:Stop()
shootAnim:Stop()
end)
humanoid.Died:Connect(function()
canReload = false
canIdle = false
canShoot = false
shooting = false
end)
equipAnim:Play()
wait(equipAnim)
if canIdle then
idleAnim:Play()
end
local function Reload()
if canReload then
ammoGui.Text = "---"
reloading = true
reloadAnim:Play()
wait(reloadAnim.Length)
ammo = maxAmmo
ammoGui.Text = ammo.." / "..maxAmmo
reloading = false
end
end
mouse.Button1Down:Connect(function()
if not canStillShoot then return end
canStillShoot = false
canShoot = true
mouse.Button1Up:Connect(function()
shooting = false
canShoot = false
canStillShoot = true
end)
if canShoot and ammo > 0 and not reloading then
canShoot = false
shooting = true
while shooting and not reloading do
ammo = ammo - 1
ammoGui.Text = ammo.." / "..maxAmmo
canShoot = false
shootAnim:Play()
humanoid.Running:Connect(function(speed)
if speed <= 0 then
isRunning = true
spread = 6
print("Running")
end
end)
if not isRunning then
if humanoid:GetState() == Enum.HumanoidStateType.None then
spread = 3
print("None")
elseif humanoid:GetState() == Enum.HumanoidStateType.Jumping or humanoid:GetState() == Enum.HumanoidStateType.FallingDown or humanoid:GetState() == Enum.HumanoidStateType.Freefall then
spread = 12
print("Jumping")
else
spread = 3
print("Else")
end
end
local ranSpreadX, ranSpreadY, ranSpreadZ = math.random(-spread, spread), math.random(-spread, spread), math.random(-spread, spread)
game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("RaycastEvent"):FireServer(script.Parent:WaitForChild("Flame").Position, mouse.Hit.Position, math.random(2,8), Vector3.new(ranSpreadX, ranSpreadY, ranSpreadZ))
wait(shootAnim.Length)
canShoot = true
end
elseif not reloading and canShoot then
Reload()
end
end)
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R and not reloading then
Reload()
end
end)
end)

2 comments
Put a debounce! Preferably right at the start of the part where you shoot.
It worked! Thanks a lot!