How does roblox default hotbar work

Not considering the UI, how can one remake roblox’s default backpack/hotbar system?

I am currently trying to write a custom hotbar similar to the roblox default one. I considered getting things from the backpack through their index, but it doesn’t really work since tools get reparented on equipment and their index will change. Another method I came up is through getting the tool’s name, but that breaks when there are items of same names. So this makes me wonder how does roblox default backpack/hotbar system work :))