Hello there, I've searched the whole YouTube, Roblox Forums etc. I have found a explanation on the Developer Hub but I can't understand what it means. What I want to ask is what is tostring
? Why do we use it and why is it helpful?
tostring()
is a function that converts (almost) any given data type to a string. There are numerous uses for it:
tostring()
. However, you need tostring()
for any other data type.For conditional purposes, tostring()
will viably compare its given data type to a string. Two examples of this:
1 | print ( tostring ( 10 ) = = "10" ) -- true |
2 | print ( tostring ( 5 * 6 > 4 ^ 2 ) = = tostring ( true )) -- true |
When converting tables to strings, it will just give the identifier instead of the table contents:
1 | print ( tostring ( { } )) -- table: <identifier> |
For concatenating numbers, tostring()
is not necessary. This print, for example...
1 | print ( 5 .. 6 ) -- 56 |
...will convert both numbers to strings before concatenating them.
tostring
is a function that converts a given number to a string. This can be useful for stuff like if
statements. If you want to compare say, a math equation to a TextBox's text, you will need to use tostring
(or tonumber
) to compare the two, as two different data types can not be compared.