Trying to make an automatic gun

I used covertcode’s gun tutorial and im trying to make it automatic but when I hold down click nothing happens, no error or anything

		repeat until mouse.Button1Up
		
			if cooldown == true  then
			return
		end
		if firetype == 2 then
			if cammo > 0 then
				cooldown = true
				pew()
				animtrack:Play()
				cammo -= 1

				wait(cdtime)
				cooldown = false
			elseif magcount > 0 then
				cooldown = true

				animtrack3:Play()
				wait(reloadtime)
				cammo = maxsize
				magcount -= 1
				cooldown = false

	
			end
		
	
		
		
			game.ReplicatedStorage.ammoupdate:FireServer(cammo, magcount, j, b)
	
	end
	end

this isnt the whole script

Shouldn’t you put the until clause below the script?
It’s just repeatedly running nothing until you stop clicking…

1 Like

i put the until on the line after the game.ReplicatedStorage.ammoupdate:FireServer(cammo, magcount, j, b) and still nothing

Edited the code for you a bit
Notes:
You don’t actually need a cooldown variable, as this is a repeat… until loop and wait automatically yields the whole loop
Don’t use return in a loop, it will end the entire script (correct me if im wrong) or the current function. Consider using continue to terminate the current literation of the loop instead

repeat 
if firetype == 2 then
if cammo > 0 then
	pew()
	animtrack:Play()
	cammo -= 1
	wait(cdtime)
elseif magcount > 0 then
	animtrack3:Play()
	wait(reloadtime)
	cammo = maxsize
	magcount -= 1
end
until mouse.Button1Up

(I assume the repeat… until loop is in a function hooked up to MouseButton1Down)

Try this for the hold. (Put it in your local script.):

local UIS = game:GetService("UserInputService")
local LMBHeld = false

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
       warn("Input Started.")
        LMBHeld = true
    end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        warn("Input Ended.")
        LMBHeld = false
    end
end)

while LMBHeld == true do
  ---Your code here
end

So this code is a function to shoot and so this code is irrelevant, the semi auto shooting works.
so now its

elseif firetype == 2 then
		repeat
			shoot()
		until mouse.Button1Down == false```
But now when I click, the gun keeps shooting after I let go of click, and if I click twice it lags a bunch
the gun only stops when it is out of ammo.

I fixed it by tying a function to mouse1up.
Probably not the best solution but it works

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.