so okey i have made a gun that works but when i reload i can spam the reload key and its annoying cuz if you ever happend to click it twice youre ammo decreases alot.
function reload(key) key:lower() local reloading = false if key == "r" and Ammo < 30 and not reloading then reloading = true Ammo = 0 game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = true spareammo = spareammo + (Ammo - 30) print("Reloading") wait(Reloadtime) print(spareammo) if spareammo > 29 then Ammo = 30 elseif spareammo < 30 and spareammo > 0 then Ammo = spareammo elseif spareammo == 0 then Ammo = 0 end game.Players.LocalPlayer.PlayerGui.weapongui.ReloadingLabel.Visible = false reloading = false end end mouse.KeyDown:connect(reload)
Although you have a debounce, since you define it inside the function, every time the function runs reloading
is set to false, making the debounce useless.
Proper debounce format:
local variable = false function func() if not variable then variable = true --code variable = false end end
I'll assume that you do indeed define mouse
earlier in the code?
It should be said, however, that instead of using that big long path to access the gui, you should shorten it with variables, at least for the player. You should also use WaitForChild
to prevent the script from erroring when the PlayerGui isn't loaded.