local function i(i)local k=Players:GetPlayerFromCharacter(i)local Q if k then Q=PLAYER_DATA:WaitForChild(k.Name)end if i then local k=i.Humanoid local y=k.Armor local T=k.ArmorSlots local Y=T.Value y.Changed:connect(function()local i=math.ceil(y.Value/SLOT_SIZE)T.Value=i end)T.Changed:connect(function()if T.Value>Y then REMOTES.Effect:FireAllClients("\066\111\111\115\116\101\114",i,"\065\114\109\111\114")elseif T.Value<Y then REMOTES.Effect:FireAllClients("\083\104\097\116\116\101\114",i,"\065\114\109\111\114")end Y=T.Value i.Armor:ClearAllChildren()if T.Value>0 then local k=CUSTOMIZATION.Armors.Default if Q then if CUSTOMIZATION.Armors:FindFirstChild(Q.Equipped.Armor.Value)then k=CUSTOMIZATION.Armors[Q.Equipped.Armor.Value]end end local y=k["\084\105\101\114"..tostring(T.Value)]:Clone()local Y=y.PrimaryPart local C=i[Y.Name]for i,k in pairs(y:GetChildren())do if k:IsA("\066\097\115\101\080\097\114\116")and k~=Y then k.CanCollide=false k.Massless=true local i=Y.CFrame:toObjectSpace(k.CFrame)local Q=Instance.new("\087\101\108\100")Q.Part0=C Q.Part1=k Q.C0=i Q.Parent=k k.Anchored=false end end Y:Destroy()y.Parent=i.Armor end end)end end local function k(k)k.CharacterAdded:connect(i)i(k.Character)end local function Q(i,k)local Q=i.Humanoid.Armor local y=i.Humanoid.ArmorSlots if Q.Value<MAX_SLOTS*SLOT_SIZE then y.Value=math.min(y.Value+k,MAX_SLOTS)Q.Value=math.min(Q.Value+k*SLOT_SIZE,MAX_SLOTS*SLOT_SIZE)return true end return false end local function y(i,k)local Q=i.Humanoid local y=Q.Armor local T=Q.ArmorSlots local Y=T.Value*SLOT_SIZE y.Value=math.min(y.Value+k*SLOT_SIZE,Y)end
-- functions

local function HandleCharacter(character)
	local player = Players:GetPlayerFromCharacter(character)
	local playerData
	if player then
		playerData = PLAYER_DATA:WaitForChild(player.Name)
	end

	if character then
		local humanoid = character.Humanoid
		local armor = humanoid.Armor
		local slots = humanoid.ArmorSlots

		local lastSlots = slots.Value

		armor.Changed:connect(function()
			local activeSlots = math.ceil(armor.Value / SLOT_SIZE)
			slots.Value = activeSlots
		end)

		slots.Changed:connect(function()
			if slots.Value > lastSlots then
				REMOTES.Effect:FireAllClients("Booster", character, "Armor")
			elseif slots.Value < lastSlots then
				REMOTES.Effect:FireAllClients("Shatter", character, "Armor")
			end

			lastSlots = slots.Value

			-- remove old armor
			character.Armor:ClearAllChildren()

			if slots.Value > 0 then
				local armor = CUSTOMIZATION.Armors.Default
				if playerData then
					if CUSTOMIZATION.Armors:FindFirstChild(playerData.Equipped.Armor.Value) then
						armor = CUSTOMIZATION.Armors[playerData.Equipped.Armor.Value]
					end
				end
				local armor = armor["Tier" .. tostring(slots.Value)]:Clone()

				local primary = armor.PrimaryPart
				local attach = character[primary.Name]

				for _, v in pairs(armor:GetChildren()) do
					if v:IsA("BasePart") and v ~= primary then
						v.CanCollide = false
						v.Massless = true

						local offset = primary.CFrame:toObjectSpace(v.CFrame)

						local weld = Instance.new("Weld")
						weld.Part0 = attach
						weld.Part1 = v
						weld.C0 = offset
						weld.Parent = v

						v.Anchored = false
					end
				end

				primary:Destroy()

				armor.Parent = character.Armor
			end
		end)
	end
end

local function HandlePlayer(player)
	player.CharacterAdded:connect(HandleCharacter)

	HandleCharacter(player.Character)
end

local function AddArmor(character, amount)
	local armor = character.Humanoid.Armor
	local slots = character.Humanoid.ArmorSlots

	if armor.Value < MAX_SLOTS * SLOT_SIZE then
		slots.Value = math.min(slots.Value + amount, MAX_SLOTS)
		armor.Value = math.min(armor.Value + amount * SLOT_SIZE, MAX_SLOTS * SLOT_SIZE)

		return true
	end

	return false
end

local function Boost(character, amount)
	local humanoid = character.Humanoid
	local armor = humanoid.Armor
	local slots = humanoid.ArmorSlots

	local maxArmor = slots.Value * SLOT_SIZE

	armor.Value = math.min(armor.Value + amount * SLOT_SIZE, maxArmor)
end
