UI Button order issue with my game [SOLVED]

,

Hello. I am working on my own inventory system for my game and got it working completely fine expect for one problem with the order of the buttons on the inventory.

The problem I am having is with the order of the buttons. When you pick up an item, the item will appear in your inventory on the first slot. But in this case, it appears on the last slot, and goes in reverse from 5-1 instead of starting from slot 1 to slot 5, 1-5.

Here’s a video to show the issue that is happening:

You can see how in the video when I pick up an item, the item goes to the last slot (Slot 5) instead of where I want it to go first (Slot1) and going up from there.

The names for buttons start with there number. So for example, the first box is named “1Slot”, the second box is named “2Slot”, etc. I thought doing it like this will help put the buttons in a order so that when the tool appears in the inventory it goes by the first number. But it seems here that it goes by the last number instead which in this case is 5.

I know one way of solving this but I’m not sure how great of an idea it is so I came here to see if anyone knows a better way I can go about solving this and having the tools appear starting from Slot 1, and going up to Slot 5.

If there is any questions you have I’ll be more than happy to answer them.

Any help is appreciated! :slight_smile:

3 Likes

i’m not sure how you’re getting the slots

if you’re using an array for the slots, you could sort it by the LayoutOrder

table.sort(Slots, function(a, b) -- Assuming slots is an array of GuiObjects
  return a.LayoutOrder < b.LayoutOrder
end)

or you could take ‘Slot’ out of the name (“1Slot” → tonumber(“1”)) and use the name as an index

local SortedSlots = {}

for _, Slot in Slots:GetChildren() do
  if not Slots:IsA("Frame")? then continue end
  local Index = tonumber(Slot.Name:gsub("Slot", ""))
  SortedSlots[Index] = Slot -- or table.insert(SortedSlots, Index, Slot)
end

there may be a better solution but this is what i thought of

1 Like

I have the slots stored in a frame and I use a UIGridLayout to organize them. I’ll try the 2nd method out in a second and tell you what happens.

Yeah I’m not having the best luck of this but I’ll keep trying. One thing is I did some tests and it does seem that the buttons are going in order, it’s just like I said though, its starting from 5 and making its way down to 1 which I want the complete opposite of that. Is there any way at all that I can just change the order of which this is going in from 5 going down to 1 to 1 going up to 5?

i already suggested using LayoutOrder or the index in the names

what exactly did you try

Ok I just did the first suggestion you told me about the LayoutOrder and I actually got it to fix the problem so now when you equip a gear, it starts from the first box, making its way up to 5.

I did a little test with it and this is what I did, Obviously I’m gonna make some changes to this, it was just a test.

I was a little confused with what exactly you did there since the stuff you used there I never used before so I’m not to familiar with it, but I was able to make some connections and came up with this.

Is this the correct way or was there a better or different way I could’ve gone about this?

i’d loop and set the parent after sorting instead of setting the parent during the sort

table.sort(Slots, function(a, b) -- Assuming slots is an array of GuiObjects
  return a.LayoutOrder < b.LayoutOrder
end)

for _, Slot in Slots do -- Slots is in order now, so you can loop
  Slot.Parent = Hotbar
end

Just did this and the issue just started happening again.

What I did:

Decided to try to reorder the slots in the table in a different way which kind of fixed the issue, but whenever I reordered the fifth slot the issue would just happen again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.