Help with stopping spam reload

Currently im working on parts for my gun, what would be the best way to stop the player from spam reloading and getting infitine ammo? This is in a ViewModel for my pistol

local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
local cam = workspace.Camera
local run = game:GetService("RunService")
local arms = game.ReplicatedFirst.FPSARMS:Clone()
local MyGun = script.Parent
local GunHandle = MyGun:WaitForChild("Handle")
local Humanoid = arms.Humanoid
local ShootAnim = arms.Shoot
local IdleAnim = arms.Idle
local ReloadAnim = arms.Reload
local gunshot = game.ReplicatedStorage.Gunshot:Clone()
local UserInputService = game:GetService("UserInputService")
local ammoCount = script.Parent.AmmoCount

gunshot.Parent = arms
arms.Parent = cam

local function reload()
	local loadReload = Humanoid:LoadAnimation(ReloadAnim)
	loadReload:Play()
	ammoCount.Value = 8
end

MyGun.Equipped:Connect(function()
	
	local FPSARMS = game.Workspace.Camera:WaitForChild("FPSARMS")
	FPSARMS.LeftArm.Transparency = 0
	FPSARMS.RightArm.Transparency = 0
	
	--game.Workspace.CurrentCamera = arms.CameraBone
	
	GunHandle.Transparency = 1
	

	local LoadAni = Humanoid:LoadAnimation(IdleAnim)
	LoadAni:Play()
	
	local GunModel = FPSARMS.GunModel:GetChildren()
	
	for i, value2 in pairs(GunModel) do
		
		value2.Transparency = 0
		
		
	end
	

	
	UserInputService.InputBegan:Connect(function(inputObject, gameProcessedEvent)
		if inputObject.KeyCode == Enum.KeyCode.R then
			reload()
		end
	end)
end)

MyGun.Activated:Connect(function()
		gunshot:Play()
		local LoadAni = Humanoid:LoadAnimation(ShootAnim)
		LoadAni:Play()
		local FPSARMS = game.Workspace.Camera:WaitForChild("FPSARMS")
		FPSARMS.LeftArm.Transparency = 0
		FPSARMS.RightArm.Transparency = 0

		GunHandle.Transparency = 1




		ammoCount.Value = ammoCount.Value - 1

		if ammoCount.Value < 1 then
			canShoot = false
			reload()
		end

		print(player.Name.." has ".. ammoCount.Value.." left in his mag")

		local GunModel = FPSARMS.GunModel:GetChildren()

		for i, value2 in pairs(GunModel) do

			value2.Transparency = 0


		end

end)

if MyGun.Equipped then
	
	arms.Parent = cam
	
	run.RenderStepped:Connect(function()
		
		arms:SetPrimaryPartCFrame(cam.CFrame * CFrame.new(0,-2,-2.5) * CFrame.Angles(0,0,0))
		
	end)	
	
	print(char)	
	
	--if char.Humanoid.GetState("Running") then
	--	print(char.Name.." is running")
	--	local runingAnim = arms.Walk
	--	local LoadAni = Humanoid:LoadAnimation(runingAnim)
	--	LoadAni:Play()
	--end
	
end



MyGun.Unequipped:Connect(function()
	
	arms.Parent = cam
	
	local FPSARMS = game.Workspace.Camera:WaitForChild("FPSARMS")
	FPSARMS.LeftArm.Transparency = 1
	FPSARMS.RightArm.Transparency = 1
	
	local GunModel = FPSARMS.GunModel:GetChildren()
	
	for i, value in pairs(GunModel) do
		
		value.Transparency = 1
		
		
	end
	
	
	
	
	
	
end)


2 Likes

Have you tried a basic cooldown system? e.g

local cooldown = false
local cooldownTime = 3
local function reload()
    if (cooldown) then return end
    cooldown = true
    delay(cooldownTime,function()
        cooldown = false
    end)

    -- reload stuff
end
2 Likes

I tried that, but was looking for something a bit easier, My script is an absolute mess ngl, so I could do it, but yeah

1 Like

What I usually do is add a reloading variable to make sure that the player can’t shoot while reloading or spam reload. I set it to true in the reload state, and for Tool.Activated I’d do a little check if the weapon is not reloading and still has ammo in the mag. If any of these conditions are false, the gun shouldn’t fire.

And for the reload function, it’s pretty much good to check if the weapon isn’t already reloading and if the ammo in the magazine is less than the magazine capacity.

Hope this helps. If you want to prevent weapons from reloading while unequipped you could :Stop() the reload animation and set reloading to false.

1 Like

What I’d do, is possibly implement a RemoteEvent check to see if that specific Player’s debounce is set to true or not (Anti-Exploit/Best Way wise)

If it is, then you could initiate the gun shooting but you’d need to pass on certain arguments to the Event when you connect/fire it

1 Like

Not sure what you’re looking for here, aren’t you trying to prevent the player from reloading a bunch of times in a short period of time? Or are you trying to limit the amount of ammo a player can have after reloading to be a set number?