Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When i reload i still can spam the reload key?

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)

2 comments

0

Try making reloading a global function and using "reloading == false" instead of "not reloading"

1

@FearMeIAmLag "not reloading" is pretty much the same as "reloading == false" in this context.

1 answer

✓ Accepted answer

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.

5 comments

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?

0

nvm its fixed now all i had to do was change "and not reloading" to "reloading == false"

0

'not reloading' and 'reloading == false' mean EXACTLY the same thing.

0

but it doesn't work with not reloading but it does with reloading == false

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

This discussion is preserved as it was. New answers, comments, and votes are closed.