ComputerCraft Forums

ComputerCraft => Ask a Pro => Topic started by: TheTinyDeskEngineer on Dec 28, 2021, 02:37 PM

Title: string value is nil even after if-else statement
Post by: TheTinyDeskEngineer on Dec 28, 2021, 02:37 PM
I'm working on a program using the Advanced Peripherals mod to make a computer lower a hidden elevator when I send a specific hidden message. However, it seems I can't concatenate a string and a boolean, and I can't use .toString() either. so I set up an if-else statement to create a string value based on what the bool is, and now sending a regular chat message displays correctly, but when I send a hidden one it says the string value is nil. I did some more testing by putting print calls in both the if and else statement, both run as expected, and I don't understand why it isn't working.

Here's the relevant code:
local event, username, message, uuid, isHidden = os.pullEvent("chat")
local stringHidden = nil
if isHidden then
    print("isHidden = true")
    stringHidden = "yes"
else
    print("isHidden = false")
    stringHidden = "no"
end
print("user: " .. username .. " message: " .. message .. " hidden: .. stringHidden)

I always get the error on the last print call.
Title: string value is nil even after if-else statement
Post by: TheTinyDeskEngineer on Dec 28, 2021, 02:45 PM
nevermind I just misspelled the name of the stringHidden variable in the if statement
Title: string value is nil even after if-else statement
Post by: TheTinyDeskEngineer on Dec 28, 2021, 02:55 PM
...And now the problem is a while loop that's used for checking if the sender is in a whitelist.

Have I forgotten where tables start in Lua again?
Title: string value is nil even after if-else statement
Post by: QuickMuffin8782 on Jan 07, 2022, 03:39 PM
Quote from: TheTinyDeskEngineer on Dec 28, 2021, 02:55 PM...And now the problem is a while loop that's used for checking if the sender is in a whitelist.

Have I forgotten where tables start in Lua again?
You might as of have. You can learn more on tables on the official Lua website for Lua 5.1, since ComputerCraft operates under that version.