How to convert userdata into string?

  1. What do you want to achieve? I want to convert an userdata into a string

  2. What is the issue? It returns “{0, 0}, {0, 0}” instead of “UDim2.new(0, 0, 0, 0)”

  3. What solutions have you tried so far? I tried tostring() but it doesn’t work.

local randomPart = Instance.new("Part")

randomPart.Size = Vector3.new(0, 0, 0)

print("v1 = "..tostring(randomPart.Size)) -- v1 = 0, 0, 0

The following code will print ‘v1 = 0, 0, 0’

But I want, ‘v1 = Vector3.new(0, 0, 0)’

How can I achieve this?

print("v1 = Vector3.new ("..tostring(randomPart.Size)..")")
1 Like

No, It can be any property, not only size.

1 Like

Before printing it, you can check if it IsA("Whatever")

local t = Vector3.new(1,1,1)
print(typeof(t)) -- Vector3
local t = BrickColor.new("Eggplant")
print(typeof(t)) -- BrickColor

I believe you could use typeof and then use more things to print whatever you want.

(If I understood you correctly)

local randomPart = Instance.new("Part")
randomPart.Size = Vector3.new(0, 0, 0)

print("v1 = "..typeof(randomPart.Size).."("..randomPart.Size.X..","..randomPart.Size.Y..","..randomPart.Size.Z..")"

So you want to be able to get a certain property then return it in a string?

1 Like

Yes. That is what I’m looking for

Thanks, I’m going to try that.

About this part,
I believe you could use string patterns to format it better, but it’s up to you.

Save yourself the hassle and just print the individual props, ex.

local Size = Vector3.new(5,10,5)
local X,Y,Z = Size.X,Size.Y,Size.Z

print(string.format("%d,%d,%d",X,Y,Z)))
1 Like

It can be UDim2, as I said, I don’t know what the property is.

Using typeof() would help you get the type of the property,

If you don’t know what property is, I think you might need to use api [or it might be impossible] to get automatically the components of a property, you might need to use formats or stuff like that.

I already made a function for it

local ConvertValueIntoString = function(Value)
    local TypeOfValue = typeof(Value)
    local Args = tostring(Value):gsub("{", ""):gsub("}", "")

    return TypeOfValue..".new("..Args..")"
end
1 Like