How do I fix my reload function it works sometimes but when you spam click while it is reloading it breaks
The code is provided below:
--Reloading
local function Reload()
if not reloading then
reloading = true
canFire = false
if ammo.Value <= 0 then
ammo.Value = 0
wait(1)
local reloadSFX = tool:WaitForChild("SFX").Reload
reloadSFX:Play()
ammo.Value = 0
plrGui.AmmoGui.Frame.AmmoLabel.Text = "Reloading.."
wait(3)
ammo.Value = maxammo.Value
plrGui.AmmoGui.Frame.AmmoLabel.Text = ammo.Value.." / "..maxammo.Value
end
reloading = false
if reloading == false then
canFire = true
end
end
end
wait(1)
ammo.Changed:Connect(Reload)
--This is written in a local script