Code works in singleplayer but not multiplayer

I’ve been working on an M1 Garand and when you reload it shows the correct numbers of bullets in the clip as you have in the gun. But when in multiplayer it always shows it as empty.



if CLIENT then
	local function M1GarandFireSet(pl, event, data)
			if event == PLAYERANIMEVENT_ATTACK_PRIMARY then
				if LocalPlayer():GetActiveWeapon():GetClass() == "tfa_pig_doi_m1_garand" then
					LocalPlayer():GetViewModel():SetBodygroup(2, LocalPlayer():GetActiveWeapon():Clip1())
				end
			end
	end

	hook.Add("DoAnimationEvent", "SetBodygroupOnFire", M1GarandFireSet)

	local function M1GarandReloadReset(pl, event, data)
			if event == PLAYERANIMEVENT_RELOAD then
				if LocalPlayer():GetActiveWeapon():GetClass() == "tfa_pig_doi_m1_garand" then
					if LocalPlayer():GetActiveWeapon():Clip1() >= 1 then
						timer.Simple( 2, function()
							if LocalPlayer():GetActiveWeapon():Ammo1() + LocalPlayer():GetActiveWeapon():Clip1() > 8 then
								LocalPlayer():GetViewModel():SetBodygroup(2, 7)
							else
								LocalPlayer():GetViewModel():SetBodygroup(2, LocalPlayer():GetActiveWeapon():Ammo1() + LocalPlayer():GetActiveWeapon():Clip1())
							end
						end )
					else
						timer.Simple( 1, function()
							if LocalPlayer():GetActiveWeapon():Ammo1() + LocalPlayer():GetActiveWeapon():Clip1() > 8 then
								LocalPlayer():GetViewModel():SetBodygroup(2, 7)
							else
								LocalPlayer():GetViewModel():SetBodygroup(2, LocalPlayer():GetActiveWeapon():Ammo1() + LocalPlayer():GetActiveWeapon():Clip1())
							end
						end )
					end
				end
			end
	end
	hook.Add("DoAnimationEvent", "ResetBodygroupOnReload", M1GarandReloadReset)
end


Yes I know the code is bad.

Correct:

Incorrect:

Any help is appreciated.



local vm
local obg = -1
local targ

SWEP.Callback = {}

SWEP.Callback.Deploy = function(self)
	obg = -1
end

SWEP.Callback.PlayerThink = function(self)
	if not IsValid(vm) then
		vm = self.OwnerViewModel
		return
	end

	if self:GetReloading() then
		targ = math.Clamp(self:Clip1() + self:Ammo1(), 0, 7)
	else
		targ = math.Clamp(self:Clip1(), 0, 7)
	end

	if obg ~= targ then
		vm:SetBodygroup(2, targ)
	end
end


Since you’re using TFA Base, this should work nicely. Though the code runs on Tick, it should be properly predicted, and only changes the viewmodel’s bodygroup when necessary.

That kind of works but I need the bullets set to a lower value when you shoot, It’s always at max capacity.