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

Setting a variable to equal a table?

Asked by
ausmel105 140
8 years ago

Hi all,

I cannot understand why this function isn't picking up the actual table names and is only altering the "tab" table within the function but not the intended table.

local PrimaryDepartures = {}
local PrimaryArrivals = {}
local SecondaryDepartures = {}
local SecondaryArrivals = {}

local PrimaryName = "Kythria"
local SecondaryName = "Skiathos"

function f.Sort()
    local tab, key, imp, times
    for i = 1 ,4 do
        if i == 1 then tab, key, imp = PrimaryDepartures, PrimaryName, "Origin"
        elseif i == 2 then tab, key, imp = PrimaryArrivals, PrimaryName, "Destination"
        elseif i == 3 then tab, key, imp = SecondaryDepartures, SecondaryName, "Origin"
        elseif i == 4 then tab, key, imp = SecondaryArrivals, SecondaryName, "Destination" end
        local times = {}
        tab = {}
        local l = 1
        for _, v in pairs(script.Parent:GetChildren()) do
            if v:IsA("Folder") and v:FindFirstChild(imp).Value == key then
                local t = tonumber(string.sub(v.Time.Value, 1, 2).."."..string.sub(v.Time.Value, 4, 5))
                times[l] = t
                tab[l] = v
                l = l + 1
            end
        end
        repeat 
            local swapped = false
            print(#tab)
            print(#PrimaryDepartures)
            for i=2, #tab do
                if times[i-1] > times[i] then
                    times[i-1], times[i] = times[i], times[i-1]
                    tab[i-1], tab[i] = tab[i], tab[i-1]
                    swapped = true
                end
            end
        until not swapped
    end
end

print(#tab) print(#PrimaryDepartures)

This results to

4 0

in the output.

It appears that the function isn't writing to the table, I don't understand as to why as I set tab = --table. I could always repeat this 4 times with each specific table, but I'd rather do this as it is more efficient. What can I do to fix this?

Thanks

Answer this question