Vector3 to String

Hey guys, I’m trying to create a BillboardGui which displays the velocity of the brick. The problem, I don’t know how to convert Vector3 values to Strings. Can you guys help me, please?

7 Likes

I know that I can just do it with X, Y, and Z separately but I was just wondering if I could do it in one piece.

You can always use tostring(value) which formats it the same way as when you print(value) it to the console.

6 Likes

Thank you so much!

einstein’s solution is 100% right, but to fill the gap in your knowledge, you can do concatenation.

It’d be written print(Vector.X … Vector.Y … Vector.Z), and would print exactly the same as what einstein suggests.

1 Like

Lua concatenation is 2 dots however :stuck_out_tongue:

Further adding to it; string.format can be used with mostly the same syntax as printf in the standard C i/o library.

http://www.cplusplus.com/reference/cstdio/printf/

print(string.format(“X is %d, Y is %d and Z is %d”,Vector.X,Vector.Y,Vector.Z))

1 Like

And is there a direct way of converting real numbers into integers and then make it a string?

I know an indirect way of creating some IntValues and just giving it’s value the real number, and then giving the string the IntValue’s value, but it’s kinda complicated.

Lua 5.1 only uses floats. Integers are not a seperate datatype. Implicitly Lua will convert a float to String whenever it is required, manually there is tonumber and tostring.

string.format("%0.f, %0.f, %0.f", v3.X, v3.Y, v3.Z)

Where v3 is a Vector3 - mind that it will try and round your numbers, instead of using ceil or floor.


To convert any value to a string, you can use tostring

tostring(value)  -- for example, local int = tostring(5) or local dec = tostring(5.1)

To convert any number to an integer (rounding down), you can use math.floor

math.floor(value)  -- for example, local int = math.floor(5.8)

You can also round up with math.ceil, or round normally using math.floor(value + 0.5)


You can combine these, for example:

local stringInt = tostring(math.floor(1.5))

If you need to be sure that it’ll appear as an integer or do any other fancy options, you can use string.format. You can see this StackOverflow question for some more info, and this reference linked there.

The following example will give you a number converted to an integer, automatically rounding down/cutting off the decimal:

local stringInt = string.format("%i", 9.7)
1 Like

Well, this was helpful. Thank you!