Reload Math not working after swapping to attribute system

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:
image

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!

Try doing it at the moment, and not saving it as a variable.

if weapon:GetAttribute("ReserveAmmo") <= 0 then
	print("No reserve ammo")
	return
end

I think what is happening is that since you get the attribute value before it is set, the number will still be the same, even after changing it. In this case, it’s better to either get the attribute after you set it, or do it during the moment.

1 Like

When you wrote local reserveAmmo = weapon:GetAttribute(“ReserveAmmo”), that took the current value of the reserveAmmo. I recommend getting the attribute each time you want to check it, since it won’t update otherwise. Therefore, as @J_Angry said, you should check the ammo at that time.

2 Likes

This worked, can’t believe I didn’t think of it lol. Thanks for the insight!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.