Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Automatically Generate Right Amount Of Buttons For Each Page?

So, I'm currently trying to make a Gui that has pages, I want to automatically make the right amount of buttons for each page, with the max amount of buttons on each page being 30.

By doing

local maxPages = math.ceil(#contents / 36)

with "contents" being the table of the items, I'm able to get the max amount of pages. Currently, I have 54 items, which would make this

local maxPages = math.ceil(54 / 36) -- equals to 2 pages

Onto the question, how would I know the exact amount of pages I would need for each page? Obviously, the first page would have 30, and the second would have 24, but how would I have the script automatically calculate this for me? I already have a script that'll make the buttons for me and position them accordingly.

2 comments

0

Sata's is right, if you don't want to do it all in a for loop, but would prefer to go one page at a time though, I would suggest you use math.min(30, #contents - 30 * pageNumber)

0

Only reason sata's might not be good is if you're doing 80000 pages and only need the first 1, then it's inefficient to do the whole for loop

1 answer

✓ Accepted answer

This is a function that should do just that for you:

local function getPagesList(Amount, maxButtons)
   local maxPages = math.ceil(Amount / maxButtons)

   local PagesList = {}
   local Remaining = Amount

   for i = 1, maxPages do 
      local maxAmount = math.min(Remaining, maxButtons)
      Remaining -= maxAmount
      
      table.insert(PagesList, maxAmount)
   end

   return PagesList
end


--Use examples:
getPagesList(54, 30) --> {30,24}
getPagesList(90, 20) --> {20,20,20,20,10}
getPagesList(12, 5) --> {5,5,2}
getPagesList(5, 10) --> {5}

Tell me if that doesn't work or if you don't understand something about the code (It's 2 am for me so I can't explain right now but hope I helped!)

This discussion is preserved as it was. New answers, comments, and votes are closed.