I swapped the weapons system in my game over from using normal values to using attributes, everything seems to be working fine asides from reloading. Sometimes it’s fine other times it just doesn’t work despite nothing seemingly changing.
Ex: Move all ammo to “Reserve” and it prints that there is for example 30 bullets in reserve, then check if reserve is greater than 0 and it acts as if reserve is less than or equal to 0 even though the player has ammo in reserve and it prints that it’s there.
Here’s my code:
game.ReplicatedStorage.Remotes.Reload.OnServerEvent:Connect(function(plr,weapon)
local function updateGUI(plr,weapon)
if weapon.Name == "Rifle" then
plr.PlayerGui.HUD.AmmoDisplay.CurrentAmmo.Text = (weapon:GetAttribute("AvailableAmmo").."/"..weapon:GetAttribute("MagSize"))
plr.PlayerGui.HUD.AmmoDisplay.AmmoType.Text = (weapon:GetAttribute("AmmoType"))
plr.PlayerGui.HUD.AmmoDisplay.RemainingAmmo.Text = plr.Ammo:FindFirstChild(weapon:GetAttribute("AmmoType")).Value
elseif weapon.Name == "Pistol" then
plr.PlayerGui.HUD.AmmoDisplay.CurrentAmmo.Text = (weapon:GetAttribute("AvailableAmmo").."/"..weapon:GetAttribute("MagSize"))
plr.PlayerGui.HUD.AmmoDisplay.AmmoType.Text = (weapon:GetAttribute("AmmoType"))
plr.PlayerGui.HUD.AmmoDisplay.RemainingAmmo.Text = plr.Ammo:FindFirstChild(weapon:GetAttribute("AmmoType")).Value
end
end
local curAmmo = weapon:GetAttribute("AvailableAmmo")
local reserveAmmo = weapon:GetAttribute("ReserveAmmo")
local totalAmmo = plr.Ammo:FindFirstChild(weapon:GetAttribute("AmmoType"))
totalAmmo.Value += curAmmo
print(totalAmmo.Value)
weapon:SetAttribute("ReserveAmmo",0)
weapon:SetAttribute("AvailableAmmo",0)
weapon:SetAttribute("ReserveAmmo",totalAmmo.Value)
print(weapon:GetAttribute("ReserveAmmo"))
weapon:SetAttribute("curAmmo",0)
task.wait(0.3)
if reserveAmmo <= 0 then
print("No reserve ammo")
return
elseif reserveAmmo <= weapon:GetAttribute("MagSize") then
print("Loading all remaining bullets")
task.wait(weapon:GetAttribute("ReloadLength"))
weapon:SetAttribute("AvailableAmmo",weapon:GetAttribute("ReserveAmmo"))
weapon:SetAttribute("ReserveAmmo",0)
totalAmmo.Value = 0
updateGUI(plr,weapon)
elseif reserveAmmo > weapon:GetAttribute("MagSize") then
print("Player has enough for a full mag!")
task.wait(weapon:GetAttribute("ReloadLength"))
weapon:SetAttribute("AvailableAmmo",weapon:GetAttribute("MagSize"))
totalAmmo.Value -= weapon:GetAttribute("MagSize")
updateGUI(plr,weapon)
end
end)
Heres a screenshot of the biggest issue which I describe above:
It also seems to enjoy acting like you have enough for a full mag when reserve is clearly showing less than magsize…
All help is appreciated!