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.
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.
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
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.