Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can some one tell me what tostring and tonumber means?

I would like to know what these words mean

To number and tostring!

1 answer

✓ Accepted answer

#tostring

The tostringfunction, as its name implies, returns its argument as a string. Take this for example:

local numberTwoAsString = tostring(2)
print(numberTwoAsString == 2) -- false, because a number cannot equal a string
print(numberTwoAsString == "2") -- true, because we converted 2 into a string

#tonumber

The tonumber function, similar to how tostring converts its argument to a string, tonumber will instead attempt to convert its argument to a number. Heavy emphasis on attempt. tonumber will return nil if its argument cannot be converted to a string. This is why it is important to be careful with tonumber, especially if you are going to perform arithmetic on something you aren't sure of being a number.

local stringFiveAsNumber = tonumber("5")
print(stringFiveAsNumber == "5") -- false, we converted 5 to a number

local thisWillBeNil = tonumber("abcdef")
print(thisWillBeNil) --> nil 

3 comments

0

Thank you incapaz. So its basicly a convertable function...?

0

tonumber() can also format a hex value into its decimal counterpart. print(tonumber("0x123ABC")) >> 1194684

0

yeah

This discussion is preserved as it was. New answers, comments, and votes are closed.