function module.CreateRanks(z)(coroutine.wrap(function()local z=main.signals.RetrieveRanksInfo:InvokeServer()local T=z.PermRanks local P=z.Ranks local E=z.Permissions local K={}local o={}for z,T in pairs(P)do local P=T[1]local E=T[2]table.insert(o,1,{P;E;{}})end for z,T in pairs(o)do K[T[1]]=z end for z,P in pairs(E)do if z=="\111\119\110\101\114"and K[8]then local z=o[K[8]]table.insert(z[3],{main.ownerName,"\079\119\110\101\114",(main:GetModule("\099\102")):GetUserImage(main.ownerId)})elseif z=="\115\112\101\099\105\102\105\099\117\115\101\114\115"then for z,T in pairs(P)do if#z>1 then local P=o[K[T]]local E={z;"\083\112\101\099\105\102\105\099 \085\115\101\114",(main:GetModule("\099\102")):GetUserImage((main:GetModule("\099\102")):GetUserId(z))}table.insert(P[3],E)end end for z,T in pairs(T)do if not T.RemoveRank then local z=o[K[T.Rank]]if z then local P={(main:GetModule("\099\102")):GetName(T.UserId);"\080\101\114\109\082\097\110\107";(main:GetModule("\099\102")):GetUserImage(T.UserId);T}table.insert(z[3],P)end end end elseif z=="\103\097\109\101\112\097\115\115\101\115"then for z,T in pairs(P)do if not(main:GetModule("\099\102")):FindValue(main.products,z)then local P=o[K[T.Rank]]if P then local E={T.Name;"\071\097\109\101\112\097\115\115";T.IconImageAssetId,tonumber(z);T.PriceInRobux}table.insert(P[3],E)end end end elseif z=="\097\115\115\101\116\115"then for z,T in pairs(P)do local P=o[K[T.Rank]]if P then local E={T.Name;"\065\115\115\101\116",(main:GetModule("\099\102")):GetAssetImage(z),tonumber(z),T.PriceInRobux}table.insert(P[3],E)end end elseif z=="\103\114\111\117\112\115"then for z,T in pairs(P)do for P,E in pairs(T.Roles)do if E.Rank then local f=o[K[E.Rank]]if f then local E={T.Name..(" \124 "..P),"\071\114\111\117\112";T.EmblemUrl;tonumber(z)}table.insert(f[3],E)end end end end elseif otherPermissions[z]then local T=otherPermissions[z]local P=E[T[1]]if P>0 then local z=o[K[P]]if z then local P={T[2],T[3],string.upper(string.sub(T[1],1,1))}table.insert(z[3],P)end end end end local f=false if main.pdata.Rank>=(main.commandRanks.permrank or 4)then f=true end;(main:GetModule("\099\102")):ClearPage(pages.ranks)local x=0 for z,T in pairs(o)do local P=T[1]local E=T[2]local K=T[3]x=x+1 local o=templates.rankTitle:Clone()o.Name="\076\097\098\101\108"..x o.RankIdFrame.TextLabel.Text=P o.RankName.Text=E o.Visible=true o.Parent=pages.ranks for z,T in pairs(K)do x=x+1 local P=T[1]local K=T[2]local o=T[3]local c=templates.rankItem:Clone()c.Name="\076\097\098\101\108"..x c.ItemTitle.Text=P c.ItemDesc.Text=K if#tostring(o)==1 then c.ItemIcon.Text=o c.ItemIcon.Visible=true c.ItemImage.Visible=false else if tonumber(o)then o="\114\098\120\097\115\115\101\116\105\100\058\047\047"..o end c.ItemImage.Image=tostring(o)end local N=.7 c.Unlock.Visible=false c.ViewMore.Visible=false if K=="\071\097\109\101\112\097\115\115"or K=="\065\115\115\101\116"then local z=T[4]local f=T[5]c.ItemImage.BackgroundTransparency=1 c.Unlock.Visible=true c.Unlock.MouseButton1Down:Connect(function()buyFrame.ProductName.Text=P buyFrame.ProductImage.Image=o buyFrame.PurchaseToUnlock.TextLabel.Text=E buyFrame.InGame.TextLabel.Text=main.gameName buyFrame.MainButton.Price.Text=(f or 0).." "buyFrame.MainButton.ProductId.Value=z buyFrame.MainButton.ProductType.Value=K;(main:GetModule("\099\102")):ShowWarning("\066\117\121\070\114\097\109\101")end)N=.4 elseif K=="\080\101\114\109\082\097\110\107"then local z=T[4]if f then local T=c.ViewMore T.Visible=true T.MouseButton1Down:Connect(function()(main:GetModule("\099\102")):ShowPermRankedUser({P;z.UserId,z.RankedBy})end)end N=.6 else N=.7 end c.ItemTitle.Size=UDim2.new(N,0,c.ItemTitle.Size.Y.Scale,0)c.ItemDesc.Size=UDim2.new(N,0,c.ItemDesc.Size.Y.Scale,0)c.Visible=true c.Parent=pages.ranks end end if x>=2 then for z=1,2,1 do local T=pages.ranks.Label1 local P=pages.ranks["\076\097\098\101\108"..x]pages.ranks.CanvasSize=UDim2.new(0,0,0,(P.AbsolutePosition.Y+P.AbsoluteSize.Y)-T.AbsolutePosition.Y)end end end))()end

function module:CreateRanks()
	coroutine.wrap(function()
		
		local ranksInfo = main.signals.RetrieveRanksInfo:InvokeServer()
		local permRanksInfo = ranksInfo.PermRanks
		local ranksToSort = ranksInfo.Ranks
		local permissions = ranksInfo.Permissions
		
		--Organise ranks
		local rankPositions = {}
		local ranks = {}
		for i, rankDetails in pairs(ranksToSort) do
			local rankId = rankDetails[1]
			local rankName = rankDetails[2]
			table.insert(ranks, 1, {rankId, rankName, {}})
		end
		for i,v in pairs(ranks) do
			rankPositions[v[1]] = i
		end
		
		for pName, pDetails in pairs(permissions) do
			--Owner
			if pName == "owner" and rankPositions[8] then
				local ownerRankDetail = ranks[rankPositions[8]]
				table.insert(ownerRankDetail[3], {main.ownerName, "Owner", main:GetModule("cf"):GetUserImage(main.ownerId)})
			--SpecificUsers and PermRanks
			elseif pName == "specificusers" then
				for plrName, rankId in pairs(pDetails) do
					if #plrName > 1 then
						local rankDetail = ranks[rankPositions[rankId]]
						local boxInfo = {plrName, "Specific User", main:GetModule("cf"):GetUserImage(main:GetModule("cf"):GetUserId(plrName))}
						table.insert(rankDetail[3], boxInfo)
					end
				end
				for i, record in pairs(permRanksInfo) do
					if not record.RemoveRank then
						local rankDetail = ranks[rankPositions[record.Rank]]
						if rankDetail then
							local boxInfo = {main:GetModule("cf"):GetName(record.UserId), "PermRank", main:GetModule("cf"):GetUserImage(record.UserId), record}
							table.insert(rankDetail[3], boxInfo)
						end
					end
				end
			elseif pName == "gamepasses" then
				--Gamepasses
				for gamepassId, gamepassInfo in pairs(pDetails) do
					if not main:GetModule("cf"):FindValue(main.products, gamepassId) then
						local rankDetail = ranks[rankPositions[gamepassInfo.Rank]]
						if rankDetail then
							local boxInfo = {gamepassInfo.Name, "Gamepass", gamepassInfo.IconImageAssetId, tonumber(gamepassId), gamepassInfo.PriceInRobux}
							table.insert(rankDetail[3], boxInfo)
						end
					end
				end
			elseif pName == "assets" then
				--Assets
				for assetId, assetInfo in pairs(pDetails) do
					local rankDetail = ranks[rankPositions[assetInfo.Rank]]
					if rankDetail then
						local boxInfo = {assetInfo.Name, "Asset", main:GetModule("cf"):GetAssetImage(assetId), tonumber(assetId), assetInfo.PriceInRobux}
						table.insert(rankDetail[3], boxInfo)
					end
				end
			elseif pName == "groups" then
				--Groups
				for groupId, groupInfo in pairs(pDetails) do
					for roleName, roleInfo in pairs(groupInfo.Roles) do
						if roleInfo.Rank then
							local rankDetail = ranks[rankPositions[roleInfo.Rank]]
							if rankDetail then
								local boxInfo = {groupInfo.Name.." | "..roleName, "Group", groupInfo.EmblemUrl, tonumber(groupId)}
								table.insert(rankDetail[3], boxInfo)
							end
						end
					end
				end
			elseif otherPermissions[pName] then
				--Other
				local info = otherPermissions[pName]
				local rank = permissions[info[1]]
				if rank > 0 then
					local rankDetail = ranks[rankPositions[rank]]
					if rankDetail then
						local boxInfo = {info[2], info[3], string.upper(string.sub(info[1], 1, 1))}
						table.insert(rankDetail[3], boxInfo)
					end
				end
			end
		end
		--Can view PermRank menu
		local canModifyPermRanks = false
		if main.pdata.Rank >= (main.commandRanks.permrank or 4) then
			canModifyPermRanks = true
		end
		
		--Clear labels
		main:GetModule("cf"):ClearPage(pages.ranks)
			
		--Setup labels
		local labelCount = 0
		for i,v in pairs(ranks) do
			local rankId = v[1]
			local rankName = v[2]
			local boxes = v[3]
			--
			labelCount = labelCount + 1
			local rankTitle = templates.rankTitle:Clone()
			rankTitle.Name = "Label".. labelCount
			rankTitle.RankIdFrame.TextLabel.Text = rankId
			rankTitle.RankName.Text = rankName
			rankTitle.Visible = true
			rankTitle.Parent = pages.ranks
			--
			for _, boxInfo in pairs(boxes) do
				labelCount = labelCount + 1
				local boxTitle = boxInfo[1]
				local boxDesc = boxInfo[2]
				local boxImage = boxInfo[3]
				local rankItem = templates.rankItem:Clone()
				rankItem.Name = "Label".. labelCount
				rankItem.ItemTitle.Text = boxTitle
				rankItem.ItemDesc.Text = boxDesc
				if #tostring(boxImage) == 1 then
					rankItem.ItemIcon.Text = boxImage
					rankItem.ItemIcon.Visible = true
					rankItem.ItemImage.Visible = false
				else
					if tonumber(boxImage) then
						boxImage = "rbxassetid://"..boxImage
					end
					rankItem.ItemImage.Image = tostring(boxImage)
				end
				local xScale = 0.7
				rankItem.Unlock.Visible = false
				rankItem.ViewMore.Visible = false
				if boxDesc == "Gamepass" or boxDesc == "Asset" then
					local productId = boxInfo[4]
					local productPrice = boxInfo[5]
					rankItem.ItemImage.BackgroundTransparency = 1
					rankItem.Unlock.Visible = true
					rankItem.Unlock.MouseButton1Down:Connect(function()
						buyFrame.ProductName.Text = boxTitle
						buyFrame.ProductImage.Image = boxImage
						buyFrame.PurchaseToUnlock.TextLabel.Text = rankName
						buyFrame.InGame.TextLabel.Text = main.gameName
						buyFrame.MainButton.Price.Text = (productPrice or 0).." "
						buyFrame.MainButton.ProductId.Value = productId
						buyFrame.MainButton.ProductType.Value = boxDesc
						main:GetModule("cf"):ShowWarning("BuyFrame")
					end)
					xScale = 0.4
				elseif boxDesc == "PermRank" then
					local record = boxInfo[4]
					if canModifyPermRanks then
						local viewMore = rankItem.ViewMore
						viewMore.Visible = true
						viewMore.MouseButton1Down:Connect(function()
							main:GetModule("cf"):ShowPermRankedUser{boxTitle, record.UserId, record.RankedBy}
						end)
					end
					xScale = 0.6
				else
					xScale = 0.7
				end
				rankItem.ItemTitle.Size = UDim2.new(xScale, 0, rankItem.ItemTitle.Size.Y.Scale, 0)
				rankItem.ItemDesc.Size = UDim2.new(xScale, 0, rankItem.ItemDesc.Size.Y.Scale, 0)
				rankItem.Visible = true
				rankItem.Parent = pages.ranks
			end
		end
		
		--Canvas Size
		if labelCount >= 2 then
			for i = 1,2 do
				local firstLabel = pages.ranks["Label1"]
				local finalLabel = pages.ranks["Label".. labelCount]
				pages.ranks.CanvasSize = UDim2.new(0, 0, 0, (finalLabel.AbsolutePosition.Y + finalLabel.AbsoluteSize.Y - firstLabel.AbsolutePosition.Y))
			end
		end
	
	end)()
end


