Chello mate!
I’m currently having problem in mein gun system.
The problem is, reloading the gun while ammo/reserve ammo less than magazine capacity will return magazine value to the max and reserve to negative number.
Here the screenshot :
Script regarding the reloading function:
reloadEvent.OnServerEvent:Connect(function(clientThatPressed)
if not isReloading.Value and (magazine.Value < refferenceMagazine.Value) and gun.Equipped and ammo.Value > 0 then
local readyReload = clientThatPressed.Character:FindFirstChild("Humanoid"):LoadAnimation(gunReload)
canFire.Value = false
isReloading.Value = true
if consolidate.Value == true then
keepRefference = refferenceMagazine.Value
readyReload:Play()
handle.GunReload:Play()
task.wait(reloadTime.Value)
calculation = (keepRefference)
ammo.Value = (ammo.Value - calculation)
magazine.Value = refferenceMagazine.Value
isReloading.Value = false
canFire.Value = true
else
keepRefference = refferenceMagazine.Value
readyReload:Play()
handle.GunReload:Play()
task.wait(reloadTime.Value)
calculation = (keepRefference - magazine.Value)
ammo.Value = (ammo.Value - calculation) -- Here the problem start!
magazine.Value = refferenceMagazine.Value
isReloading.Value = false
canFire.Value = true
end
end
end)
reloadEvent.OnServerEvent:Connect(function(clientThatPressed)
if not isReloading.Value and (magazine.Value < refferenceMagazine.Value) and gun.Equipped and ammo.Value > 0 then
local readyReload = clientThatPressed.Character:FindFirstChild("Humanoid"):LoadAnimation(gunReload)
canFire.Value = false
isReloading.Value = true
if consolidate.Value == true then
keepRefference = refferenceMagazine.Value
readyReload:Play()
handle.GunReload:Play()
task.wait(reloadTime.Value)
calculation = (keepRefference)
if ammo.Value >= calculation then -- added this line
ammo.Value = (ammo.Value - calculation) -- moved this line inside the if statement
end -- added this line
magazine.Value = refferenceMagazine.Value
isReloading.Value = false
canFire.Value = true
else
keepRefference = refferenceMagazine.Value
readyReload:Play()
handle.GunReload:Play()
task.wait(reloadTime.Value)
calculation = (keepRefference - magazine.Value)
if ammo.Value >= calculation then -- added this line
ammo.Value = (ammo.Value - calculation) -- moved this line inside the if statement
end -- added this line
magazine.Value = refferenceMagazine.Value
isReloading.Value = false
canFire.Value = true
end
end
end)