Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: attempt to index field '?' (a nil value) [closed]

Tags:

lua

I get the following error in my Lua code:

attempt to index field '?' (a nil value)

It happens on the line below in bold. How can I fix it?

function SendMessageToAdmins(color1, color2, color3, msg)
    for i = 0, maxSlots - 1 do
        if Account[i] and Account[i].Admin >= 1 or Account[i] and Account[i].GameMaster >= 1 then
            SendPlayerMessage(i, color1, color2, color3, string.format("%s", msg))
        end
    end
end
like image 730
user1629374 Avatar asked Dec 12 '25 17:12

user1629374


1 Answers

this error usually comes from trying to index a field on something that isn't a table, or nil. chances are that whatever is at Account[i] when the error happens, isn't a table or userdata, but a built in type like a string or number.

i'd start with checking the type of whatever is in Account[i] when you get that error, and going from there.

the two most common ways to see this error (that i know of) are below:

local t = { [1] = {a = 1, b = 2}, [2] = {c = 3, d = 4} }
-- t[5] is nil, so this ends up looking like nil.a which is invalid
-- this doesn't look like your case, since you check for 
-- truthiness in Account[i]
print(t[5].a)

the case you are probably experiencing, is most likely this one:

local t =
{
    [1] = {a = 1, b = 2},
    [2] = 15, -- oops! this shouldn't be here!
    [3] = {a = 3, b = 4},
}
-- here you expect all the tables in t to be in a consistent format.
-- trying to reference field a on an int doesn't make sense.
print(t[2].a)
like image 163
Mike Corcoran Avatar answered Dec 14 '25 15:12

Mike Corcoran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!