Why won't this gun stop shooting?

Hello, I have made a script for an automatic gun. It is meant to carry on shooting and stop shooting as soon as the mouse button is lifted. This is not the case, the gun does not stop shooting and it carries on until the ammo goes far into minues

mouse.Button1Down:Connect(function()
    isdown = true
end)

mouse.Button1Up:Connect(function()
    isdown = false
end)

while true do
    if debounce == false then
        debounce = true
    if ammo > 0 then
        local victim = mouse.Target.Parent
        local damage = math.random(10,23)
        script.Parent.events.Fired:FireServer(victim, damage)
        wait(0.3)
            debounce = false
        else
            reload()
            end
    end
end

Your loop is wrong, you only put while true do, while true do what? You need to specify, which in your case would be while isdown do. While true will just run forever until it crashes because it’s always true.

Spoonfeeding, this is really unhelpful and does not help the person learn anything at all, they have made their own gun system, and just need a simple fix, no need to basically tell them their code is trash and to use this copy-pasted script instead.

No need to argue, I’m just giving a well-known fact, this doesn’t teach them anything.

You are missing an end in your statement, (right under your if debounce == false)
Also, your script will time out while the player has no ammo. Don’t forget to add a task.wait() right before the loop ends.
You also don’t check the isdown variable after if ammo > 0, this means that as soon as the gun is equipped it will never stop shooting.

1 Like

no. that way the gun will stop forever

mouse.Button1Down:Connect(function()
    isdown = true
end)

mouse.Button1Up:Connect(function()
    isdown = false
end)

while task.wait() do -- instead of true, use wait.
    if debounce == false then debounce = true end
    if (not isdown) then continue end
    if (ammo > 0) then
        local victim = mouse.Target.Parent
        local damage = math.random(10,23)
        script.Parent.events.Fired:FireServer(victim, damage)
        task.wait(0.3)
        debounce = false
    else
        reload()
    end
end

1 Like

Is there a difference between task.wait() and wait()?

task.wait is more efficient than wait

1 Like

Yes, task.wait() is more accurate compared to wait()
That’s why I use task.wait() instead of wait()

1 Like

This thread is not being productive whatsoever. Only the first few posts made progress to solving this.

I’m recommending that you properly format your code so that it’s a little more readable.

I see a few issues with your code. First of all, you can completely remove the debounce. I also see that you assign the isdown variable globally but you don’t use it.

Fix #1 (recommended): Place the loop into the Button1Down function, after isdown = true. Change while true do to while isdown do. This will cause the gun to shoot only until isdown is percieved as false. To prevent issues, add the line if isdown then return end before isdown = true to prevent the user from spamming the gun.

Fix #2: Add the line if not isdown then task.wait(.1) continue end at the start of the while true do loop, which will prevent the gun from shooting if the mouse is not down. This is the easier solution, but it may not be the most performant.

1 Like

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