--This script exists to let you add armor to mobs and also, to auto re-equip armor on respawn.


function ArmorModule.GetLevel(armor)
	if game.ReplicatedStorage.ARMORS:FindFirstChild(armor) ~= nil then
		return game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Level.Value
	else
		return 0 --If it's not found, returns 0.
	end
end

function ArmorModule.GetDefense(armor)
	if game.ReplicatedStorage.ARMORS:FindFirstChild(armor) ~= nil then
		if game.Workspace.GAME_PROPERTIES.DefenseMode.Value == 1 or game.Workspace.GAME_PROPERTIES.DefenseMode.Value == 2 then
			return game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Defense.Value
		elseif game.Workspace.GAME_PROPERTIES.DefenseMode.Value == 3 then
			return game.ReplicatedStorage.ARMORS:FindFirstChild(armor).HPBoost.Value
		else
			return 0
		end
	else
		return 0 --If the armor is not found, returns 0.
	end
end

function ArmorModule.RemoveArmor(character)
	local humanoid = character:FindFirstChild("Humanoid") or character:FindFirstChild("Human")
	--Remove old armour.
	if humanoid ~= nil then
		if character:FindFirstChild("Arm1") ~= nil then
			character:FindFirstChild("Arm1"):Remove()
		end
		if character:FindFirstChild("Arm2") ~= nil then
			character:FindFirstChild("Arm2"):Remove()
		end	
		if character:FindFirstChild("Leg1") ~= nil then
			character:FindFirstChild("Leg1"):Remove()
		end
		if character:FindFirstChild("Leg2") ~= nil then
			character:FindFirstChild("Leg2"):Remove()
		end	
		if character:FindFirstChild("Chest") ~= nil then
			character:FindFirstChild("Chest"):Remove()
		end	
	else
		return false --Exits the function if it's nil. Prevents the function from breaking.
	end	
end

function ArmorModule.EquipArmor(character, armor)
	local humanoid = character:FindFirstChild("Humanoid") or character:FindFirstChild("Human")
	if character ~= nil then
		--Remove old armour.
		if humanoid ~= nil then
			if character:FindFirstChild("Arm1") ~= nil then
				character:FindFirstChild("Arm1"):Remove()
			end
			if character:FindFirstChild("Arm2") ~= nil then
				character:FindFirstChild("Arm2"):Remove()
			end	
			if character:FindFirstChild("Leg1") ~= nil then
				character:FindFirstChild("Leg1"):Remove()
			end
			if character:FindFirstChild("Leg2") ~= nil then
				character:FindFirstChild("Leg2"):Remove()
			end	
			if character:FindFirstChild("Chest") ~= nil then
				character:FindFirstChild("Chest"):Remove()
			end	
		end
	else
		return false --Exits the function if it's nil. Prevents the function from breaking.
	end
	--Arms
	if humanoid ~= nil and character:FindFirstChild("Arm1") == nil then
		local g = game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Arm1:Clone()
		g.Parent = character
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].ClassName == "Part" then
				if C[i].Name == "Middle" then
					C[i].Transparency = 1
				end
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = character["Left Arm"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end

	if humanoid ~= nil and character:FindFirstChild("Arm2") == nil then
		local g = game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Arm2:Clone()
		g.Parent = character
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].ClassName == "Part" then
				if C[i].Name == "Middle" then
					C[i].Transparency = 1
				end
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = character["Right Arm"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end
	--Legs
	if humanoid ~= nil and character:FindFirstChild("Leg1") == nil then
		local g = game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Leg1:Clone()
		g.Parent = character
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].ClassName == "Part" then
				if C[i].Name == "Middle" then
					C[i].Transparency = 1
				end
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = character["Left Leg"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end

	if humanoid ~= nil and character:FindFirstChild("Leg2") == nil then
		local g = game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Leg2:Clone()
		g.Parent = character
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].ClassName == "Part" then
				if C[i].Name == "Middle" then
					C[i].Transparency = 1
				end
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = character["Right Leg"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end
	--Torso
	if humanoid ~= nil and character:FindFirstChild("Chest") == nil then
		local g = game.ReplicatedStorage.ARMORS:FindFirstChild(armor).Chest:Clone()
		g.Parent = character
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].ClassName == "Part" then
				if C[i].Name == "Middle" then
					C[i].Transparency = 1
				end
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = character.Torso
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			h[i].Anchored = false
			h[i].CanCollide = false
		end
		
	end
end

return ArmorModule