Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When i reload i still can spam the reload key?

Asked by
Kryddan 261 Moderation Voter
10 years ago

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)
0
Try making reloading a global function and using "reloading == false" instead of "not reloading" FearMeIAmLag 1161 — 10y
1
@FearMeIAmLag "not reloading" is pretty much the same as "reloading == false" in this context. Redbullusa 1580 — 10y

1 answer

Log in to vote
3
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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.

0
yes mouse is defined and local reloading = false i missed when i copied the code. it lays right above the function. and WaitForChild i will fix later but you got something else that might work? Kryddan 261 — 10y
0
nvm its fixed now all i had to do was change "and not reloading" to "reloading == false" Kryddan 261 — 10y
0
'not reloading' and 'reloading == false' mean EXACTLY the same thing. Perci1 4988 — 10y
0
but it doesn't work with not reloading but it does with reloading == false Kryddan 261 — 10y
0
Probably a typo, or some other explanation, because 'not boolean' and 'boolean == false' are the same thing. You can test this with simpler examples if you wish. http://wiki.roblox.com/index.php?title=Writing_Clean_Code#Booleans Perci1 4988 — 10y
Ad

Answer this question