Attempt to index boolean

Hello, excuse me if I have made a very stupid mistake, but I keep on getting an error that I’m trying to index a boolean when it is very clearly a table I am trying to access. I have even type checked the value to make sure it is indeed a table.

local function initialise(tasks)
    for _, this_task in pairs(tasks) do
        print(type(this_task) == "table")

        if this_task.init then
            this_task:init()
        end

        this_task.__initialised = true
    end
end

return initialise

Despite this, the value somehow still becomes a boolean according to the error outputted ? Any help is appreciated.

EDIT: To add onto this, this error only occurs when the key indexed does not exist in the able, however, I would still not understand why this error is happening as it should simply just return nil.

2 Likes

What line does the error happen at?

Its whenever I try to index “this_task” so it would be this line that first causes trouble.

That’s really bizarre. Try putting a breakpoint and screenshot the stack.

Hello, sorry for the late response.

The values seem to check out as a table.

Without breakpoint:

Seems to think that this_task is a boolean somehow…

Have you tried adding a print(_, “:”, task) within the for loop? Maybe the break point is a bit of a red herring and stopping before the error actually would occur.

Yes, I have tried doing that. Though the loop literally errors on the first iteration. If I put it before the line where it tries to index this_task it shows me that it is a table, any point afterwards and nothing is shown because it errors.

Is init method modified anywhere else? Though I don’t think it’s related to the problem… I tried to recreate a similar scenario like this and it worked.

1 Like

Just to be sure, does your this_task table have any metatables?

4 Likes

Very strange. A dodgy fix for this would be to try and wrap the init call in a pcall.

Another thing you could try is something like:

local initFunction = (type(this_task) == “table”) and task_task.init 

if initFunction then 
    initFunction(this_task)
end

I know this should make no difference in a practical sense but may produce a slightly different error that may provide a clue

That’s strange… Even this seems to produce the same error I am having. Since the first operand was true, that would lead us to believe that is definitely a table… Though it still seems that when we get to the second operand, this_task somehow turns into a boolean value…

1 Like

That’s very strange. It passes the first check but not the second one?

Yep, that’s correct. It passes the first operand completely fine.

This is not the issue right? The variable name is wrong here.

1 Like

No that’s not the issue, I noticed his spelling mistake and corrected it accordingly in my own script.

Let me repeat what @takticiadam said since you didn’t answer this question.

It’s possible it has a metatable with an __index field that produces this error message. __index only runs if the key table is trying to get doesn’t exist, hence

this_task cannot turn into a boolean.

1 Like

Oh, I apolagise, I didn’t even see his message. It turns out that it is indeed related to a metatable which I set to this_task with an __index feild. It all makes sense now. Such a stupid mistake for me to have make. Thank you to those who spent their time replying.