Armor/Shield bar not replenishing correctly

As you can see from the video below the armor bar works for when it’s just replenishing itself while the players’ health is full. But if the players’ health is not full then when the player goes to pick up an armor boost it gives it to the health first and not all to the armor and I’m not sure why that is.

This is the code that is in a ServerScript

local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local armorRE = replicatedStorage:WaitForChild("armor")
local armorBar = replicatedStorage:WaitForChild("armorBar")

local armorMax = 100

function updateGui(gui, armorAmount)
	local y = armorAmount / armorMax
	gui:WaitForChild("container"):WaitForChild("bar").Size = UDim2.new(y, 0, 1, 0)
end

local function playerAdded(player)
	local function characterAdded(character)

		for _, v in ipairs(player:WaitForChild("PlayerGui"):WaitForChild("MainScreenGui"):WaitForChild("CanvasGroup"):WaitForChild("playerBars"):GetChildren()) do
			if v.Name == "armorBar" then
				v:Destroy()
			end
		end


		local humanoid = character:WaitForChild("Humanoid")
		local newGui = armorBar:Clone()
		newGui.Parent = player:WaitForChild("PlayerGui"):WaitForChild("MainScreenGui"):WaitForChild("CanvasGroup"):WaitForChild("playerBars")

		local armor = armorMax
		local oldHealth = humanoid.Health

		updateGui(newGui, armor)

		humanoid.HealthChanged:Connect(function(newHealth)
			if armor > 0 and newHealth < oldHealth then
				local change = oldHealth - newHealth

				humanoid.Health = humanoid.MaxHealth
				armor = armor - change

				if armor < 0  then
					armor = 0
				end

				local healthMinus = math.clamp(armor, -humanoid.MaxHealth, 0)
				updateGui(newGui, math.clamp(armor, 0, armorMax))

				oldHealth = humanoid.MaxHealth + healthMinus
				humanoid.Health = oldHealth
			end
		end)

		armorRE.Event:Connect(function(armorAmount, armorItem)
			if armor >= 100 then
				return
			end

			armorItem:Destroy()
			armor += armorAmount

			if armor >= 100 then
				armor = 100
			end

			armor = math.floor(armor)
			updateGui(newGui, armor)

		end)

		task.spawn(function()
			while true do
				wait()
				print(armor)
			end
		end)
	end

	if player.Character then
		characterAdded(player.Character)
	end

	player.CharacterAdded:Connect(characterAdded)
end

for _, player in ipairs(players:GetPlayers()) do
	playerAdded(player)
end

players.PlayerAdded:Connect(playerAdded)

It is replenishing your health instead of your armor because you are setting humanoid.Health to your max health every time.
The error is located here

Even after taking that line out the armor still disappears and the health refills. No idea why.