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


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
