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

For loop producing nil value?

Asked by 6 years ago
Edited 6 years ago

Using a function in a module script, I am getting a nil value that is confusing the lights out of me. Can someone help me debug this? I am using this function to produce a 6 digit string/integer key for a table index. The zVal is nil when passed to the function for some reason. the xVal was passed properly, however.

Error: ReplicatedStorage.MazeDataGenerator:63: attempt to compare number with nil

Module Script:

function module:GetIndexKey(xVal, zVal)
    local xStr = ''
    local zStr = ''

--zVal is nil, even though 0 was passed in the argument; added this if statement prevent nil error
    if xVal ~= nil and zVal ~= nil then 
        if xVal >= 0 and xVal <= 9 then
            xStr = "00" .. tostring(xVal)
        elseif xVal > 9 and xVal <= 99 then
            xStr = "0" .. tostring(xVal)
        else
            xStr = xVal
        end

        if zVal >= 0 and zVal <= 9 then -- The error occurs here if I don't have the nil check
            zStr = "00" .. tostring(zVal)
        elseif zVal > 9 and zVal <= 99 then
            zStr = "0" .. tostring(zVal)
        else
            zStr = tostring(zVal)
        end

    else
        print ("nil values in xVal or zVal")
        print (xVal)
        print (zVal)
    end
    return (xStr .. zStr)
end

Calling function:

for x = 0,_sizeX do
        for z = 0,_sizeZ do
            -- Here, z = 0 in the first loop
            local indexKey = mazeDataGenerator.GetIndexKey(x, z)            
            print(indexKey)
            if mazeData[indexKey] == 0 or mazeData[indexKey] == nil then
                msg = msg .. "..."
            else
                msg = msg .. "=="           
            end
        end
        msg = msg .. "\n"
    end
    print(msg)
0
Check out https://developer.roblox.com/articles/Understanding-Functions-in-Roblox for additional information, btw. I suggest looking at "Functions Within Tables" and "Methods". They're different types of functions. I forgot to include that in my answer, it's pretty important stuff! EzraNehemiah_TF2 3552 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

The problem

is the way that you've written the function in your module script and how you are calling it in your other script

--Module
function module:GetIndexKey(xVal, zVal)
--Other script
local indexKey = mazeDataGenerator.GetIndexKey(x, z)

Notice how in your module, your function is written as :GetIndexKey() with a colon, but your other script has it as .GetIndexKey with a period. They need to match up.


Solutions

--Module
function module.GetIndexKey(xVal, zVal)
--Other script
local indexKey = mazeDataGenerator.GetIndexKey(x, z)

--Other solution:

--Module
function module:GetIndexKey(xVal, zVal)
--Other script
local indexKey = mazeDataGenerator:GetIndexKey(x, z)

You could also just put a random value before x in your function, like mazeDataGenerator.GetIndexKey(0, x, z), but I would not recommend at all.



Hope it helps!

0
That was it! thanks! I'd upvote, but need some more rep yet. Simple things these bugs can be Karl_Poppa 11 — 6y
Ad

Answer this question