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

This code will print a value, but returns nil when asked to return the exact same value?

Asked by
Trew86 175
5 years ago
local selectRoute = function(train, model)

    local route = train.PrimaryPart.Values.RouteValue.Value
    local given = model:FindFirstChild("GivenPath")

    local greenLitPaths = {}
    local yellowLitPaths = {}

    --some code omitted for this post

    local function searchPathOptions()
--Omitted for this post

    local function findBestPath()
        searchPathOptions()
        if #greenLitPaths == 1 then
            local array = greenLitPaths[1]
            local finalPath = array[1]
            print(finalPath) 
            return(finalPath) 
        elseif #greenLitPaths > 1 then 
            sort(greenLitPaths)
            local array = greenLitPaths[1]
            local finalPath = array[1]
            print(finalPath) --this prints what I expect
            return(finalPath) --this returns nil. why??????
        elseif #greenLitPaths < 1 then
            if #yellowLitPaths == 1 then
                local array = yellowLitPaths[1]
                local finalPath = array[1]
                print(finalPath)
                return(finalPath)
            elseif #yellowLitPaths > 1 then
                sort(yellowLitPaths)
                local array = yellowLitPaths[1]
                local finalPath = array[1]
                print(finalPath)
                return(finalPath)
            end
        end
        resetTables()
    end

    if given ~= nil then
        if given.Values.BlockingTrain.Value == nil then
            findBestPath()
        end
    else
        findBestPath()
    end

end

--start the path-choosing process
module.initiateChoice = function(train, model)
    print(train, model)
    local chooseRoute = selectRoute(train, model)
    print(chooseRoute)
    repeat 
        wait()
    until chooseRoute ~= nil
        local affected = chooseRoute:FindFirstChild("AffectedPaths")
        local light = model.Values.Light.Value
        local status = model.Values.Status
        status.Value = chooseRoute.Values.Status.Value
        switchMod.setSwitches(chooseRoute.Switches)
        light.TrackDisplay.SurfaceGui.TextLabel.Text = chooseRoute.Values.ToTrack.Value
        module.exist(affected, function(object)
        module.alertAffected(train, object)
        end)
end

note the lines where I say "this prints what I expect". On this line, I ask to print a member of a table, and it prints what I want it to (an object named Path). However, in the next line, I ask to return the exact same value, yet chooseRoute(variable in initiateChoice) always comes out to nil. This is really puzzling me.

0
I got rid of the parentheses in the return lines and I still have the same problem. Trew86 175 — 5y

Answer this question