Model is nil, even though a function creates it

In my function, it clones a random pre-set model and returns the model, however it returns the model as nil. I want to know how and why the model is set to nil even after the function returns the object value. function in question vvvv

function choosehallway(j,i)
    local num = math.random(1, #hallwaychildren)
    local qawrestdryhouijmp = false
    local g
    local function f()
        local hallway = hallwaychildren[num]:Clone()
        hallway.Parent = Tiles
        hallway.Name = #Tiles:GetChildren()
        hallway.PrimaryPart = hallway.Floor
        Grid[#Grid + 1] = hallway
        hallway:PivotTo(CFrame.new(TileSize.X*j,Origin.Y,TileSize.Z*i))
        qawrestdryhouijmp = true
        return hallway
    end
    
    --btw, grid[#grid] is the previous grid space, not the current one.
    --grid[#grid + 1] is the current space, grid[#grid + 1 + gridX] is the one below the current 
    --one, grid[#grid + 1 - gridX] is the space above the current space, and grid[#grid + 2]
    --is the space after the current space.
    --by current space I mean the space we are choosing rn.
    
    if Grid[#Grid] then --if not first, then...
        if Grid[#Grid]:FindFirstChild("E") == nil and hallwaychildren[num]:FindFirstChild("W") == nil then
            if qawrestdryhouijmp == false then
                g = f()
            end
        elseif Grid[#Grid + 1 - GridX] then
            if Grid[#Grid + 1 - GridX]:FindFirstChild("N") == nil and hallwaychildren[num]:FindFirstChild("S") == nil then
                if qawrestdryhouijmp == false then
                    g = f()
                end
            end
        elseif Grid[#Grid + 1 + GridX] then
            if Grid[#Grid + 1 + GridX]:FindFirstChild("S") == nil and hallwaychildren[num]:FindFirstChild("N") == nil then
                if qawrestdryhouijmp == false then
                    g = f()
                end
            end
        elseif Grid[#Grid + 2] then
            if Grid[#Grid + 2]:FindFirstChild("W") == nil and hallwaychildren[num]:FindFirstChild("E") == nil then
                if qawrestdryhouijmp == false then
                    g = f()
                end
            end
        else
            local hallway = Hallways.SNWE:Clone()
            hallway.Parent = Tiles
            hallway.Name = #Tiles:GetChildren()
            hallway.PrimaryPart = hallway.Floor
            Grid[#Grid + 1] = hallway
            hallway:PivotTo(CFrame.new(TileSize.X*j,Origin.Y,TileSize.Z*i))
            qawrestdryhouijmp = true
            g = hallway
        end
    else --if first then...
        g = f()
    end
    
    if g == nil then
        wait(.1)
        return choosehallway(j,i)
    else
        return g
    end
end

I tried changing the order and ensuring the function returns a model every time it is called (somehow it still returned nil??). I even tried forcing the function to be continuously called until it didn't return nil, which also didn't work for whatever reason. It just stops without an error every time the function returns nil, with the rest of the map not generating. There also seems to be a random grid space all the way out in the middle of nowhere, idk why.

image of generation example

Edit: fixed it. new code vvvv

function choosehallway(j,i)
    local num = math.random(1, #hallwaychildren)
    local qawrestdryhouijmp = false
    local g
    local function f()
        local hallway = hallwaychildren[num]:Clone()
        hallway.Parent = Tiles
        hallway.Name = #Tiles:GetChildren()
        hallway.PrimaryPart = hallway.Floor
        Grid[#Grid + 1] = hallway
        hallway:PivotTo(CFrame.new(TileSize.X*j,Origin.Y,TileSize.Z*i))
        qawrestdryhouijmp = true
        return hallway
    end
    
    local function s(thing, dir, dir2)
        local Space = Grid[#Grid + thing]
        if Space ~= nil then
            if Space:FindFirstChild(dir) == nil and hallwaychildren[num]:FindFirstChild(dir2) == nil then
                return true
            end
        end
    end
    
    --btw, grid[#grid] is the previous grid space, not the current one.
    --grid[#grid + 1] is the current space, grid[#grid + 1 + gridX] is the one below the current 
    --one, grid[#grid + 1 - gridX] is the space above the current space, and grid[#grid + 2]
    --is the space after the current space.
    --by current space I mean the space we are choosing rn.
    
    local hc = hallwaychildren[num]
    
    g = f()
    if s(0, "E","W") == true then
        return g
    else
        if s(GridUp, "S","N") == true then
            return g
        else
            if s(GridDown, "N","S") == true then
                return g
            else
                if s(GridRight, "W","E") == true then
                    return g
                else
                    wait(.1)
                    g.Parent = nil
                    g = choosehallway(j,i)
                    return g
                end
            end
        end
    end
end


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation