CAS Function ignoring cooldown, allowing it to be spammed

Heya there.

I’m working on a simple gun for a fan recreation game called “Noobs vs Zombies: Relish Reborn.”. I’ve added a reload function whenever a player ran out of ammo to fire. The issue is that the function can be spammed. I tried adding in a cooldown to prevent it from being spammed although it completely ignores it.

It’s my first time using ContextActionService, so don’t mind me, I guess.

--//Functionality
local CAS = game:GetService("ContextActionService")
--Getting player's mouse
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

--//Reloading
local ReloadCD = false
local function Reloading()
	local ammoContain = Tool:GetAttribute("Ammo_Contain")
	local ammoHold = Tool:GetAttribute("Ammo_Hold")
	
	if not ReloadCD then
		ReloadCD = true
		
		--//Checking to see if AmmoHold is less.
		if Tool:GetAttribute("Ammo_Hold") <= AmmoHold then

			--//We're also checking to see if they have enough ammo to even reload it.
			if Tool:GetAttribute("Ammo_Contain") >= 1 then
				--//Reloading Animation
				local ReloadAnim = Player.Character.Humanoid:FindFirstChild("Animator"):LoadAnimation(AnimationFolder.Reload)
				ReloadAnim:Play()

				ReloadAnim:GetMarkerReachedSignal("RackedGunIndication"):Connect(function()
					Tool.Pistol.Reload:Play()
				end)

				ReloadAnim.Stopped:Wait()

				Tool:SetAttribute("Ammo_Hold", 12)
				Tool:SetAttribute("Ammo_Contain", ammoContain - 12)
				FireEvent:FireServer("Reload")

				print("Ammo Remaining: "..ammoContain.."... Better use it wisely.")
				ReloadCD = false
			else
				warn("You don't got ammo!! Go near an ammo box or find an ammo noob to refill!!")
				ReloadCD = false
				return
			end
		else
			print("Your gun is already loaded! Silly!!")
			ReloadCD = false
			return
		end
	end
end
CAS:BindAction("Reload",Reloading,false,Enum.KeyCode.R)

Add the if statement above the GetAttribute statements. They may take a slight amount of time to run:

local function Reloading()
	--if the player is reloading, don't run anything
	if ReloadCD then return end
	ReloadCD = true
	local ammoContain = Tool:GetAttribute("Ammo_Contain")
	local ammoHold = Tool:GetAttribute("Ammo_Hold")
	--rest of your code here
end

Doesn’t work sadly.

filler

Ill go bump this again.

assaasd

I’ve looked at the code, and just from a quick glance the only possible way this could be happening is if the function is evaluating Tool:GetAttribute("Ammo_Hold") <= AmmoHold to be false, which would mean it would instantly undo the cooldown. You should look into that attribute. It’s the cause, for sure.