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

Is There Any Way For Me to Make This Script Shorter With Loops?

Asked by
8391ice 91
7 years ago

The script below is supposed to be for an Inventory GUI. When an item lands in a folder, the folder's script, which is shown below, moves it to an available spot. However, I plan to add more than just 3 rows to the inventory, which would make this script incredibly long, and I can't help but think there's a more efficient way to type all of this. Is there a way?

script.Parent.ChildAdded:connect(function(item)
    local checkInfo = item:FindFirstChild("Info") --Checks to see if it is a proper item.
    if checkInfo ~= nil then
        local body = script.Parent.Parent.Frame
        local r1 = body.Row1 --The rows are just rows in the inventory; they each contain boxes for the item called slots.
        local r2 = body.Row2
        local r3 = body.Row3
        if r1.Occupied.Value ~= true then
            if r1.T1.Occupied.Value ~= true then
                item.Parent = r1.T1.Item
            elseif r1.T1.Occupied.Value == true and r1.T2.Occupied.Value ~= true then
                item.Parent = r1.T2.Item
            elseif r1.T1.Occupied.Value == true and r1.T2.Occupied.Value == true and r1.T3.Occupied.Value ~= true then
                item.Parent = r1.T3.Item
            end
        elseif r1.Occupied.Value == true and r2.Occupied.Value ~= true then
            if r2.T1.Occupied.Value ~= true then
                item.Parent = r2.T1.Item
            elseif r2.T1.Occupied.Value == true and r2.T2.Occupied.Value ~= true then
                item.Parent = r2.T2.Item
            elseif r2.T1.Occupied.Value == true and r2.T2.Occupied.Value == true and r2.T3.Occupied.Value ~= true then
                item.Parent = r2.T3.Item                
            end
        elseif r1.Occupied.Value == true and r2.Occupied.Value == true and r3.Occupied.Value ~= true then
            if r3.T1.Occupied.Value ~= true then
                item.Parent = r3.T1.Item
            elseif r3.T1.Occupied.Value == true and r3.T2.Occupied.Value ~= true then
                item.Parent = r2.T2.Item
            elseif r3.T1.Occupied.Value == true and r3.T2.Occupied.Value == true and r3.T3.Occupied.Value ~= true then
                item.Parent = r3.T3.Item                
            end
        elseif r1.Occupied.Value == true and r2.Occupied.Value == true and r3.Occupied.Value == true then
            print("no room")
            item:Destroy()
        end
    end
end)
1
I recommend using a two dimensional table. And you don't need all the extra occupied checks when using elseif. GoldenPhysics 474 — 7y
0
Thank you! 8391ice 91 — 7y

Answer this question