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

What is "tostring" used for?

I've seen a lot scripts using "tostring". I was trying to understand what it means but I didn't figured out yet. Help please.

1 comment

0

tostring(1) turns something into a string.

1 answer

✓ Accepted answer

It makes things into strings
As a cat, I prefer yarn.

tostring simply takes something and returns the string interpretation of it. It's used behind the scenes for print, and isn't necessary in a lot of cases because most things have implicit typecasting in Lua.

Tables and userdata both have their __tostring metamethod invoked if applicable, and it uses the string interpretation of whatever is returned. I'm unsure of whether nil being returned is turned into 'nil' or whether it just gets the memory value for the object at that point, however.

2 comments

0

Thank you!

0

tostring is allowed to return non-string values. If you try to print something that does that, though, you get an error.

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