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)