Saving UDim2 Value and then Loading it

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to save a UDim2 Value and then load it
  2. What is the issue? Include screenshots / videos if possible!
    The saving works but i cannot seem to unpack it correctly it gives nil
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local Value = UDim2.new(0.5, 0, -1.5, 0)  
local Table = {Value} 
local New = unpack(Table)
print(New)  -- this prints out {0.5, 0}, {-1.5, 0}
local UdimTest = UDim2.new(New) 
print(UdimTest) -- but here its nil

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Seems like a lot of issue for nothing. The code you posted can be fixed and shortened to this.

local  UdimTest = UDim2.new(0.5, 0, -1.5, 0) 
print(UdimTest)

I wanna save the data and then load it

Could be because of

Meaning

Is basically
UDim2.new({0.5, 0}, {-1.5, 0})
And I think it should be without those wiggly brackets.

But yeah, @JarodOfOrbiter is right.

Any idea how can i get rid of them or another way i can save and load the data?

Is it possible to save ‘UdimTest’ directly?

UDim2 is not serialisable so you need to save the individual values. In this case the problem is that the table you want to later unpack consists of the UDim2 itself not its values. You just need to change the composition of your table to get this working.

local Value = UDim2.fromScale(0.5, -1.5)
local Table = {Value.X.Scale, Value.Y.Scale}
local UDimTest = UDim2.fromScale(table.unpack(Table))
print(UDimTest)

Essentially what happened with your original code was that you were trying to create a UDim2 with a UDim2, or something to that extent. You put a UDim2 into a table and then unpacked it, thus getting back that same UDim2, not its X and Y values.

I don’t know how arbitrary you wanted this so I just changed it to use fromScale. Additionally its important that you unpack in the UDim2 constructor because table.unpack returns a tuple so assigning only one variable will only catch the first value out of the unpack.

1 Like

I dont think so ( random words for char limit)