Securing KeyCodes for Buttons

Hello,

So for my game, i want to use KeyCode instead of always needing to click buttons.
However this is not very secure due to the LayoutOrder. Means that Button 1 (From list should have KeyCode 1) however button 2 activates.


So how can i do it better or secure it

Before creating the buttons, put the price data (that determines layoutorder) into a table.

local datatable = {};
for i, stat in items? do
   table.insert(datatable, stat);
end;

Then sort the table using table.sort.

table.sort(datatable, function(a,b)
   return (-a.Price)>(-b.Price);   

   -- might be wrong on the operator here.
   -- depending on if your sorting is ascending or descending,
   -- you might need to use < instead of >.   
end);

Loop through the table and create the buttons from that.

for i, e in datatable do
   local layoutOrder = e;
   local keycodeNum = i;
      
   local button = <button path>:Clone();
   button.Price.Text = e.Price;
   button.LayoutOrder = layoutOrder;
   local keycodeValue = Instance.new("NumberValue");
   keycodeValue.Name = "KeyCode";
   keycodeValue.Value = keycodeNum;
end;
1 Like