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


