Automatic gun not working

My script is supposed to make an automatic gun fire for as long as the mouse is held down, but that doesn’t happen, nothing happens. Anyone able to help?

wait(0.001)
local mouseDown = false
local Mouse = game.Players.LocalPlayer:GetMouse()

script.Parent.Equipped:Connect(function()
	holdAnim:Play()
	textLabel.Parent.Enabled = true

	Mouse.Button1Down:Connect(function()
		mouseDown = true
	end)

	Mouse.Button1Up:Connect(function()
		mouseDown = false
	end)
    
    while mouseDown do
		wait(0.01)
		if Ammo > 0 and not reloading then
			--Do stuff
	end
end)

Try replacing your script with this:

wait(0.001)
local mouseDown = false
local Mouse = game.Players.LocalPlayer:GetMouse()
local Equipped = false

script.Parent.Equipped:Connect(function()
	holdAnim:Play()
	textLabel.Parent.Enabled = true
	Equipped = true
end)

script.Parent.Unequipped:Connect(function()
	Equipped = false
end

Mouse.Button1Down:Connect(function()
	mouseDown = true
end)

Mouse.Button1Up:Connect(function()
	mouseDown = false
end)

while wait(0.01) do
	if mouseDown and Equipped then
		if Ammo > 0 and not reloading then
			--Do stuff
	end
end

Your problem was, as soon as the tool is equipped, it sees that mouseDown is false and skips over the while loop. The way while loops work is when the condition is met, it runs the code until the condition is not met, then doesn’t run again. That’s why it just never runs.

What I have done is use a while loop that runs all the time, but only does “stuff” when mouseDown is true and the tool is equipped.
I’ve also moved the while loop and mouse down/up events completely out of the equipped function so they don’t run multiple times after the tool has been equipped multiple times, and added an “Equipped” variable to tell the while loop whether or not the tool is equipped so that it doesn’t let you fire while the tool isn’t equipped.

FYI: You might have to add some stuff to the Unequipped event, like setting the textLabel.Parent.Enabled property to false, etc. However I can’t see the rest of your script so I don’t know if you’re already doing that. Also, any code after the while loop will not run, so you might want to put it in a new coroutine like this:

coroutine.wrap(function()
	while wait(0.01) do
		if mouseDown and Equipped then
			if Ammo > 0 and not reloading then
				--Do stuff
		end
	end
end)()

That causes the while loop to run on a separate thread, allowing code after it to be executed.

If you need any more help let me know. Hope I could help!

Here’s my full script now. Doesn’t work. I’m probably doing something wrong.

wait(0.001)
local mouseDown = false
local maxAmmo = 30
local Ammo = maxAmmo
local reloading = false
local player = game.Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local textLabel = playerGui:WaitForChild("HK416Ammo"):FindFirstChild("AmmoText")
local humanoid = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
local animator = humanoid:FindFirstChildOfClass("Animator")
local reload = animator:LoadAnimation(script.Parent.Anims:FindFirstChild("Reload"))
local holdAnim = animator:LoadAnimation(script.Parent.Anims:FindFirstChild("Hold"))
local Mouse = game.Players.LocalPlayer:GetMouse()
local Equipped = false

Mouse.Button1Down:Connect(function()
	mouseDown = true
end)

Mouse.Button1Up:Connect(function()
	mouseDown = false
end)

local function reloadFunc()
	local reloadSound = Instance.new("Sound")
	reloadSound.SoundId = "rbxassetid://144798533"
	reloadSound.Parent = script.Parent
	reloadSound.Playing = true
	reloadSound.PlaybackSpeed = 1.5
	reloading = true
	reload:Play()
	wait(2.5)
	Ammo = maxAmmo
	reloading = false
end

script.Parent.Equipped:Connect(function()
	holdAnim:Play()
	textLabel.Parent.Enabled = true
	Equipped = true

	local input = game:GetService("UserInputService")
	input.InputBegan:Connect(function(Key)
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and Ammo ~= maxAmmo then
			reloadFunc()
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	holdAnim:Stop()
	textLabel.Parent.Enabled = false
	Equipped = false
end)

while wait(0.1) do
	if Equipped and mouseDown then
		while mouseDown do
			wait(0.01)
			if Ammo > 0 and not reloading then
				Ammo = Ammo - 1
				local sound = Instance.new("Sound")
				sound.SoundId = "rbxassetid://168143115"
				sound.Parent = script.Parent
				sound.Playing = true
				if Mouse.Target.Parent:FindFirstChild("Humanoid") then
					script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 15)
				end
			elseif reloading == false then
				reloadFunc()
			end

			while wait() do
				textLabel.Text = Ammo.." / "..maxAmmo
			end
		end
	end
end

You get one bullet and nothing else.

I can see that you’ve used way more while loops than needed. Try this instead:

while wait(0.01) do
	if Equipped and mouseDown then
		if Ammo > 0 and not reloading then
			Ammo = Ammo - 1
			local sound = Instance.new("Sound")
			sound.SoundId = "rbxassetid://168143115"
			sound.Parent = script.Parent
			sound.Playing = true
			if Mouse.Target.Parent:FindFirstChild("Humanoid") then
				script.Parent.DealDamage:FireServer(Mouse.Target.Parent, 15)
			end
		elseif reloading == false then
			reloadFunc()
		end
		textLabel.Text = Ammo.." / "..maxAmmo
	end
end
1 Like