Testing for nil/empty

In HaasScript and Lua, you can test for nil or empty values using conditional statements. Here’s how to do it in both languages:

HaasScript

HaasScript is based on Lua but has its own functions and specificities. To test for nil or empty values in HaasScript, you can use a conditional statement like this:

local variable = ""

if variable == nil or variable == "" then
    Log("Variable is nil or empty")
else
    Log("Variable has a value: " .. variable)
end

Here, we’re checking if variable is nil (not assigned a value) or an empty string. If either condition is true, the log message will indicate that the variable is nil or empty. Otherwise, it will log the value of the variable.

Lua

In Lua, you can use a similar conditional statement to test for nil or empty values:

local variable = ""

if variable == nil or variable == "" then
    print("Variable is nil or empty")
else
    print("Variable has a value: " .. variable)
end

This Lua code snippet works the same way as the HaasScript example. The main difference is that we use the print() function to output the result, whereas HaasScript uses the Log() function.

Remember to adapt these examples to your specific use case, and make sure to consider the data type of the variable you’re testing. The examples above are tailored to testing string variables, but you can adjust the conditions to check for other data types like tables, numbers, or custom objects.

Back to: HaasScript Fundamentals > Debugging and Troubleshooting