if not tonumber(tArgs[1]) then
print("Pulse interval must be numerical.")
error("Usage: <pulse interval> <side1> <side2>...", 0)
end
That should work fine, if you want you can put newlines in error messages
if not tonumber(tArgs[1]) then
error("Pulse interval must be numerical.\nUsage: <pulse interval> <side1> <side2> ...", 0)
end
for i,v in ipairs(tArgs) do
if i > 1 then
if tArgs[i] ~= ("left" or "right" or "top" or "bottom" or "front" or "back") then
error("Side must be top, bottom, left, right, back or front", 0)
end
end
end
This would not work, and you immediately need to get yourself out of this habit. The section
("left" or "right" or "top" or "bottom" or "front" or "back")
does not work how you think it does, what it does it it resolves all the values, and in Lua a non-nil or non-false will resolve to true, so the statement would end up looking like so
if tArgs[i] ~= (true or true or true or true or true or true) then
now with
Boolean OR logic we can see that all those true's resolve down to a single true
true OR true = true
true OR false = true
false OR true = true
false OR false = false
Once Lua has resolved them (since you places brackets around them, it will now compare against
tArgs[1]. Resulting in the if statement
if tArgs[i] ~= true then
causing it to error as tArgs
will not be true
There are two ways you can do what you wish to do…
#1
if tArgs[i] ~= "left" and tArgs[i] ~= "right" and tArgs[i] ~= "top" and tArgs[i] ~= "bottom" and tArgs[i] ~= "back" and tArgs[i] ~= "back" then
Note we use and here, since we want it to error if all of them don't match.
Boolean AND logic
true AND true = true
true AND false = false
false AND true = false
false AND false = false
#2
The other way, is we can setup a lookup table and check against it like so
local validSides = {
left = true, --# note it can be any value here except false or nil, it doesn't need to be true
right = true,
top = true,
bottom = true,
front = true,
back = true,
}
--# the later when validating
if not validSides[tArgs[i]] then
error("Not a valid side", 0)
end