I’m trying to save the size of a frame, but it’s not possible to save Udim2, how do I do it?
CLIENT:
SERVER:
ERROR:
I’m trying to save the size of a frame, but it’s not possible to save Udim2, how do I do it?
CLIENT:
SERVER:
ERROR:
Hi. Datastores cannot store data types like these, so you need to use serialization. The solution for you could be to use these two functions. They can convert Udim2 to a table and vice versa.
function UDim2ToTable(value : UDim2) : {number}
local x = value.X
local y = value.Y
return {x.Scale, x.Offset, y.Scale, y.Offset}
end
function TableToUDim2(value : {number}) : UDim2
return UDim2.new(value[1], value[2], value[3], value[4])
end
Although, the best solution would be to use some serialization module. Like this.
Through serialization. Serialization is the process of reducing complex abstract data structures into the least amount of primitive information required to reconstruct (deserialize) the data structure. Roblox cannot save your UDim2
because it contains functions etc. The only relevant details are its offset and scale values on the X and Y axis. You can save them as a CSV (comma-separated values) string, which is the most memory efficient:
local function serializeUDim2CSV(udim2: UDim2): string
local x = udim2.X
local y = udim2.Y
local values = {
x.Scale, x.Offset,
y.Scale, y.Offset,
}
return table.concat(values, ",")
end
local function deserializeUDim2CSV(csv: string): UDim2
local values = string.split(csv, ",")
return UDim2.new(table.unpack(values))
end
How do you separate all 4 values at once and turn them into Udim2?
Call DESERIALIZE:
DESERIALIZE:
TABLE:
ERROR CODE:
You’re not supposed to use tables. CSV stands for comma-separated values, which is a string
Can u do some example for my code?
You give the UDim2
to serializeUDim2CSV
and it produces a string for you to save. You reconstruct the UDim2
with deserializeUDim2CSV
by giving it the saved string and applying the UDim2
to your GUI
yes but the saved string is inside of the table, i need to deserialize four times?
you people complicate things so much
Just pack it into an array of XScale
, XOffset
, YScale
, YOffset
local udimTuple = {
ud2.X.Scale,
ud2.X.Offset,
ud2.Y.Scale,
ud2.Y.Offset
}
Then unpack it directly into the UDim2 constructor
UDim2.new(unpack(udimTuple))
If you’re trying to get a string, you should also consider HttpService.JSONEncode
since that will give you a fairly good serialised string format from a table. Though I’m pretty sure DataStores will accept a table directly.
["Break"] = {
["Position"] = "0.568,0,0.663,0",
["Size"] = "0,100,0,100",
-- ...
}
Your data should look like the above. If I understand your schema correctly, the serialization process would look like:
serializedGuis[gui.Name] = {
Position = serializeUDim2CSV(gui.Position),
Size = serializeUDim2CSV(gui.Size)
}
The deserialization process would look like:
for guiName, data in serializedGuis do
local gui = -- Whatever you do here...
gui.Position = deserializeUDim2CSV(data.Position)
gui.Size = deserializeUDim2CSV(data.Size)
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.