Hello!
I have this issue when I am trying to load a save file. It gives me an error saying that I cannot compare a string to a number, and I obviously know that's the problem. Is there any way I can convert a string to a number, or possibly declaring the variable a number in the save? Sorry if that sounded confusing, this is my first forum post.
Strings to numbers
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Strings to numbers
Try adding # before the variable. If it was in a table i believe it should work out like that.
Re: Strings to numbers
I thought tonumber(a) serves this purpose, and # is for element count in the table. Am I wrong?
Re: Strings to numbers
You can use "tonumber".
Should work with decimal numbers or hex numbers.
Returns nil if it fails to convert the number.
Turns out that "#s" for strings is used to get the number of bytes, similar to "string.len":
Code: Select all
assert(type(s) == "string", "input must be a string")
n = tonumber(s)
assert(type(n) == "number", "conversion failed!")
Returns nil if it fails to convert the number.
Turns out that "#s" for strings is used to get the number of bytes, similar to "string.len":
Code: Select all
a = "012345"
assert(#a == 6, "string is not 6 bytes long!")
Note that for UTF8 strings, the number of bytes may not equal the number of characters.The length operator is denoted by the unary operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte).
Who is online
Users browsing this forum: Bing [Bot] and 79 guests