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.
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.
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…
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
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.