Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My automatic gun will not work?

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

0

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

1 answer

On line 139 it shows what the values of ranSpreadX, ranSpreadY, and ranSpreadZ are. Just replace the ending part of the variable with = 5 or however fast you want the bullet to be going. If this didn't work let me know.

2 comments

1

What I meant with the gun going very fast, is that the bullet just come out multiple times whenever the gun is being spammed. Thanks for the reply anyways!

0

Try using debounce, sorry this answer didn't help.

This discussion is preserved as it was. New answers, comments, and votes are closed.