My automatic gun will not work?
Asked by
3 years ago Edited 3 years ago
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):
005 | local shooting = false |
006 | local canReload = false |
007 | local isRunning = false |
009 | local canStillShoot = true |
017 | local reloading = false |
025 | local character = player.Character or player.CharacterAdded:Wait() |
026 | local playerGui = player:WaitForChild( "PlayerGui" ) |
029 | local humanoid = character:WaitForChild( "Humanoid" ) |
032 | local gunGui = playerGui:WaitForChild( "GunGui" ) |
033 | local mainFrame = gunGui:WaitForChild( "MainFrame" ) |
034 | local ammoGui = mainFrame:WaitForChild( "Ammo" ) |
039 | local equipAnim = humanoid:LoadAnimation(script.Parent:WaitForChild( "Equip" )) |
040 | local idleAnim = humanoid:LoadAnimation(script.Parent:WaitForChild( "Idle" )) |
041 | local shootAnim = humanoid:LoadAnimation(script.Parent:WaitForChild( "Shoot" )) |
042 | local reloadAnim = humanoid:LoadAnimation(script.Parent:WaitForChild( "Reload" )) |
049 | ammoGui.Text = ammo.. " / " ..maxAmmo |
053 | script.Parent.Unequipped:Connect( function () |
066 | humanoid.Died:Connect( function () |
081 | local function Reload() |
087 | wait(reloadAnim.Length) |
090 | ammoGui.Text = ammo.. " / " ..maxAmmo |
095 | mouse.Button 1 Down:Connect( function () |
096 | if not canStillShoot then return end |
097 | canStillShoot = false |
099 | mouse.Button 1 Up:Connect( function () |
106 | if canShoot and ammo > 0 and not reloading then |
110 | while shooting and not reloading do |
112 | ammoGui.Text = ammo.. " / " ..maxAmmo |
118 | humanoid.Running:Connect( function (speed) |
126 | if not isRunning then |
127 | if humanoid:GetState() = = Enum.HumanoidStateType.None then |
130 | elseif humanoid:GetState() = = Enum.HumanoidStateType.Jumping or humanoid:GetState() = = Enum.HumanoidStateType.FallingDown or humanoid:GetState() = = Enum.HumanoidStateType.Freefall then |
139 | local ranSpreadX, ranSpreadY, ranSpreadZ = math.random(-spread, spread), math.random(-spread, spread), math.random(-spread, spread) |
141 | game.ReplicatedStorage:WaitForChild( "RemoteEvents" ):WaitForChild( "RaycastEvent" ):FireServer(script.Parent:WaitForChild( "Flame" ).Position, mouse.Hit.Position, math.random( 2 , 8 ), Vector 3. new(ranSpreadX, ranSpreadY, ranSpreadZ)) |
143 | wait(shootAnim.Length) |
147 | elseif not reloading and canShoot then |
152 | userInputService.InputBegan:Connect( function (input) |
153 | if input.KeyCode = = Enum.KeyCode.R and not reloading then |