Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help on table sorting system by number?

Asked by 2 years ago
Edited 2 years ago

So im making a game and im making the inventory system depending on how many items there are on the items folder and their type, and i want that the buttons shown up in the inventory are group color coded, like first i want the type 1 item buttonss then the type 2 item buttons and so on. But im struggling to make it so ill be very grateful if you guys help me with it.

Here is the code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local items = ReplicatedStorage:WaitForChild("Items")   
local itemsTable = items:GetChildren()
local itemsTableName = {}

local InventoryStuff = script.Parent.InventoryStuff
local ButtonScript = script.PlaceItem

local Y = 0
local CurrentPosY = UDim.new(0, Y)
local PosX = UDim.new(0.034, 0)
local size = UDim2.new(0.9, 0, 0, 50)
local addY = 100

local function GetColorButton(ItemType)
    local ButtonColor = nil

    if ItemType == 1 then
        ButtonColor = Color3.fromRGB(170, 255, 255)
    elseif ItemType == 2 then
        ButtonColor = Color3.fromRGB(255, 255, 127)
    elseif ItemType == 3 then
        ButtonColor = Color3.fromRGB(255, 0, 0)
    elseif ItemType == 4 then
        ButtonColor = Color3.fromRGB(255, 170, 0)
    end

    return ButtonColor
end

local function CreateButton(Name, ItemType)
    local GuiButton = Instance.new("TextButton")
    local Round = Instance.new("UICorner", GuiButton)
    local ButtonColor = GetColorButton(ItemType)

    GuiButton.Text = Name
    GuiButton.TextScaled = true
    GuiButton.Name = Name
    GuiButton.Size = size
    GuiButton.Position = UDim2.new(0.034,0,0, Y)
    GuiButton.BackgroundColor3 = ButtonColor
    Y += addY
    GuiButton.Parent = InventoryStuff
end

for i = 1, # itemsTable do
    itemsTableName[itemsTable[i].Name] = itemsTable[i].Type.Value
end

print(table.concat(itemsTableName, ", "))

table.sort(itemsTableName, function(a,b)
    return a < b
end)

print(table.concat(itemsTableName, ", "))

table.foreach(itemsTableName, function(key,value)
    local CurrenItemName = key
    local CurrentItem = items:FindFirstChild(CurrenItemName)
    local ItemType = CurrentItem:FindFirstChild("Type")

    if not ItemType then
        warn("no item type")
    end

    CreateButton(CurrenItemName, ItemType.Value)
end)

for i = 1, #InventoryStuff:GetChildren() do
    local Buttons = InventoryStuff:GetChildren()

    ButtonScriptClone = ButtonScript:Clone()
    ButtonScriptClone.Parent = Buttons[i]
    ButtonScriptClone.Disabled = false
end

Answer this question