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 extremely fast. Is there any way fix to this? Any answer is appreciated!
Script (You might have to press view source for this one):
--[[ Values ]]--
-- Booleans
local canIdle = true
local canShoot = true
local shooting = false
local canReload = false
local isRunning = false
local canStillShoot = true
-- Numbers
local spread = 0
-- Ammo
local maxAmmo = 30
local ammo = maxAmmo
local reloading = false
--[[ Math ]]--
--[[ Services ]]--
local userInputService = game:GetService("UserInputService")
--[[ Player Variables ]]--
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local playerGui = player:WaitForChild("PlayerGui")
-- Character
local humanoid = character:WaitForChild("Humanoid")
-- Gui
local gunGui = playerGui:WaitForChild("GunGui")
local mainFrame = gunGui:WaitForChild("MainFrame")
local ammoGui = mainFrame:WaitForChild("Ammo")
--[[ Variables ]]--
local mod = script.Parent:WaitForChild("Model")
local equipAnim = humanoid:LoadAnimation(script.Parent:WaitForChild("Equip"))
local idleAnim = humanoid:LoadAnimation(script.Parent:WaitForChild("Idle"))
local shootAnim = humanoid:LoadAnimation(script.Parent:WaitForChild("Shoot"))
local reloadAnim = humanoid:LoadAnimation(script.Parent:WaitForChild("Reload"))
--[[ Functions ]]--
--[[ Script ]]--
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)

1 comment
im gonna be honest, i kinda gave up when I saw you had 157 lines. Can you shrink the script a bit?