Page Calculator

This is for page systems such as those that use UIGridLayout

Ever needed to calculate how many pages you would need for a certain number of objects?

The Script
-- GetPages() Created by @FlaringSkies.

local function GetPages(Items, Debug)
	--Template Pages: GetPages(Amount : number, DebugLogging : boolean)
	local ItemNumber = tonumber(Items)
	local CurrentNumber = ItemNumber
	local SlotsPerPage = 4
	local Pages = 0
	
	if Debug == nil then
		Debug = false
	end
	
	local function Log(Text, Type)
		if Debug == true then
			if Type == 1 then
				print(Text)
			end
			if Type == 2 then
				warn(Text)
			end
		end
	end
	
	if ItemNumber ~= nil then
		if ItemNumber >= 0 then
			while CurrentNumber > 0 do
				if CurrentNumber - SlotsPerPage > 0 then
					Pages += 1
					CurrentNumber -= SlotsPerPage
				else
					CurrentNumber = 0
					Pages += 1
					break
				end
			end
			local EmptySpaces = (SlotsPerPage*Pages)-ItemNumber
			Log(("[#"..tostring(ItemNumber).."]: Objects need ".. Pages.. " pages and ".. EmptySpaces.. " empty spaces."), 1)
			return Pages, EmptySpaces
		else
			Log(("[#"..tostring(ItemNumber).."]: Failed to get \"ItemNumber\" argument was less than 0."),2)
		end
	else
		Log(("[#"..tostring(ItemNumber).."]: Failed to get \"ItemNumber\" argument returned nil."),2)
	end
end

How do I use this?

Start off by changing the “SlotsPerPage” variable to how many slots you have on each page, for me, I set it to 4 because there is only 4 slots on my page.

Next, if you want a simple return of how many pages and empty spaces you can use this:

local Pages, EmptySpaces = GetPages(AmountOfObjects: number, OPTIONAL: DebugPrinting : boolean)
-- Returns: number of pages, number of empty spaces

If you have any more questions about this feel free to ask!

Did you find this resourceful?

  • Yes
  • No

0 voters

1 Like

I think this unnecessarily complicates basic mathematical operations.

To determine the required number of pages, we can use the operation:

math.ceil(amountOfObjects / objectsPerPage)

This equation divides the total number of items by the number of items per page and rounds up the result to the nearest integer using the math.ceil method. This ensures that any remaining items beyond a full page are accommodated on an additional page.

To determine if there are any empty spaces on the last page, we can use the operation:

objectsPerPage - (amountOfObjects % objectsPerPage)

Here, % denotes the modulus operator, which calculates the remainder when dividing the total number of items (amountOfObjects) by the number of items per page (objectsPerPage). The difference between the number of items per page and the remainder gives us the number of empty spaces on the last page.

7 Likes

I didn’t know about this thx for letting me know!

1 Like