Search code examples
xmlluatabletop-simulator

Tabletop Simulator Lua Error: "attempt to perform arithmetic on a string value"


First, a bit of context (in addition to the fact that I'm fairly new to coding)... I'm developing a board game, and for the Tabletop Simulator version of said game, I've created a series of UI menu actions that pop up whenever a player presses a button in the middle of the table (the player must first announce that they'll be submitting a bid for a particular job, and their button press initiates an RFP bidding process for that job). After the player presses the button, the UI delivers a prompt asking all other players if they'd like to participate in bidding for the job in question.

Bid intention menu

All participating bidders then enter their effective bid——along with their market reputation, which affects the overall fee they'll collect from the job if they win the bidding process——and then the script finds the lowest bid to determine the job winner.

Bid adjustment/submission menu

The first two fields of this bid adjustment menu are editable, while the third (the fee) is read-only. The fee number adjusts in real-time according to whatever values have been entered into the other two. The formula for calculating the fee is effective bid + (reputation * 5) = fee.

The issue I'm experiencing is that whenever a player deletes a value from one of the first two fields (e.g., by highlighting it and pressing backspace), the following error pops up in the game's chat box:

Error in Script (Custom Assetbundle - 64453f) function <adjustReputationBlue>:
chunk_3:(555,4-55): attempt to perform arithmetic on a string value

I was working under the assumption that the error is caused by a nil value as soon as the numerical value is removed, so I tried adding some conditional logic that resets nil values in the first two fields to 0 before any arithmetic is performed. That didn't work, however, so I'm a bit stumped.

Here's the relevant Lua code (which is in the button's ttslua file):

function adjustBidBlue(Player, value, id)
    effective_bid_blue = value
    feeAdjustBlue()
end
function adjustReputationBlue(Player, value, id)
    reputation_blue = value
    feeAdjustBlue()
end
function feeAdjustBlue()
    if effective_bid_blue == nil then
        effective_bid_blue = 0
    end
    if reputation_blue == nil then
        reputation_blue = 0
    end
    fee_blue = effective_bid_blue + reputation_blue * 5
    UI.setAttribute("fee_blue", "text", fee_blue)
end

And here's the relevant XML code (which is in the global UI file):

<InputField
    id="effective_bid_blue"
    active="false"
    characterLimit="4"
    characterValidation="Integer"
    fontSize="26"
    height="44"
    offsetXY="-285 -150"
    onValueChanged="64453f/adjustBidBlue"
    shadow="rgba(0,0,0,0.5)"
    shadowDistance="2 -2"
    showAnimation="FadeIn"
    animationDuration="0.25"
    visibility="Blue"
    width="85">
</InputField>

<InputField
    id="reputation_blue"
    active="false"
    characterLimit="2"
    characterValidation="Integer"
    fontSize="26"
    height="44"
    offsetXY="-95 -150"
    onValueChanged="64453f/adjustReputationBlue"
    shadow="rgba(0,0,0,0.5)"
    shadowDistance="2 -2"
    showAnimation="FadeIn"
    animationDuration="0.25"
    visibility="Blue"
    width="85">
</InputField>

<InputField
    id="fee_blue"
    active="false"
    characterValidation="Integer"
    fontSize="26"
    height="44"
    offsetXY="95 -150"
    readOnly="true"
    shadow="rgba(0,0,0,0.5)"
    shadowDistance="2 -2"
    showAnimation="FadeIn"
    animationDuration="0.25"
    width="85"
    visibility="Blue">
</InputField>

If you'd like to see the full code, here it is on GitHub. And here it is in the Steam workshop.

Thanks so much!


Solution

  • As Joseph pointed out, effective_bid_blue = value and reputation_blue = value needed to be changed to effective_bid_blue = tonumber(value) and reputation_blue = tonumber(value), since the "value" param variable becomes a string whenever it's not an entered number or nil.