Bloblobbob
Bloblobbob

Reputation: 1

My lua code produces the error "Object tried to call nil" when trying to check if a player has a certain trait [PZ]

I'm attempting to mod a game I've been playing recently, and I've encountered an error when writing some code that adds a new mechanic. The problem itself isn't very complicated, but I can't seem to figure out what the solution is in this context. I'm pretty new to coding, especially with lua, so I apologize if the solution is really obvious. I've looked around for some answers, but again, nothing I can find seems to help me in this specific context. The game I'm trying to mod is called Project Zomboid, if that helps.

When attempting to start the game with the mod enabled, the error "Object tried to call nil in isBloodthirsty" pops up. Here's the snippet of code that's causing the error:

local function isBloodthirsty(player)
    if player:getDescriptor():getTrait() ~= "bloodthirsty" then
        return false else
        return true
    end
end

Ignoring how poorly written it is, the code was supposed to check if the player had a certain trait, and if so, then set the value isBloodthirsty to true. I think the error is caused by lua not recognizing the value "bloodthirsty", but I'm not sure what I should put instead. If anybody has an idea of what I'm doing wrong, I'd greatly appreciate some help. If it's helpful, I can post the rest of the code.

Upvotes: 0

Views: 787

Answers (1)

Bloblobbob
Bloblobbob

Reputation: 1

Thanks to all the great help from the stack overflow community, I managed to figure out what my problem was. The code I had written wasn't working because lua didn't recognize "bloodthirsty" as a valid trait string. My solution was to mix up the code a bit and frame the trait as a profession instead (a profession is kind of like a collection of traits within the game). The following code worked:

local function bloodthirstyStart(player)
    if player:getDescriptor():getProfession() ~= "bloodthirsty" then
        return
    end

Upvotes: 0

Related Questions