Cannot put UDim2's and Color3.fromRGB in tables

Hello guys, recently I’ve been trying to return all properties of an instance, but for some odd reason, I’m unable to put Color3.fromRGB and UDim2 values inside a table. Whenever I try printing them, it shows three values and doesn’t display them in their usual format (e.g., 0,0,0 instead of UDim2.new(0,0,0) ).

Here’s my code

		local function ReturnAllProperties(TheInstance)
			local AllowedProperties = {
				Size = UDim2.new(TheInstance.Size.X.Scale,TheInstance.Size.X.Offset,TheInstance.Size.Y.Scale,TheInstance.Size.Y.Offset) or nil,
				Position = UDim2.new(TheInstance.Position.X.Scale,TheInstance.Position.X.Offset,TheInstance.Position.Y.Scale,TheInstance.Position.Y.Offset) or nil,
				BackgroundTransparency = tonumber(TheInstance.BackgroundTransparency) or nil,
				Rotation = tonumber(TheInstance.Rotation) or nil,
				BackgroundColor3 = Color3.fromRGB(TheInstance.BackgroundColor3.R,TheInstance.BackgroundColor3.G, TheInstance.BackgroundColor3.B) or nil,
				BorderColor3 = Color3.fromRGB(TheInstance.BorderColor3.R,TheInstance.BorderColor3.G,TheInstance.BorderColor3.B) or nil,
				TextScaled = TheInstance.TextScaled or nil,
				Font = TheInstance.Font.Name or nil,
				Text = tostring(TheInstance.Text) or nil,
			}
			
			return AllowedProperties
		end
2 Likes

You don’t need to break the size and colors into separate pieces:

local function ReturnAllProperties(TheInstance)
	local AllowedProperties = {
		Size = TheInstance.Size or nil,
		Position = TheInstance.Position or nil,
		BackgroundTransparency = tonumber(TheInstance.BackgroundTransparency) or nil,
		Rotation = tonumber(TheInstance.Rotation) or nil,
		BackgroundColor3 = TheInstance.BackgroundColor3 or nil,
		BorderColor3 = TheInstance.BorderColor3 or nil,
		TextScaled = TheInstance.TextScaled or nil,
		Font = TheInstance.Font.Name or nil,
		Text = tostring(TheInstance.Text) or nil,
	}

	return AllowedProperties
end
2 Likes

How is the or nil statement useful in this case?

If a property doesn’t make the statement true it means it’s either nil or false, if its nil you don’t have to convert it to nil, and if its false it will change the property value(from false to nil). So it’s both redundant and may cause issues with trying to save/return bool values.

2 Likes

I will try it now and see if it works

First, if you are meaning printing to the Output by saying display, the Output won’t show them as a Color3.fromRGB() or UDim2.new(). It will only output the values themself.

If you are just trying to use the Properties from one Instance for another Instance, the current code will work regardless of your output. @Super_God567 code should work for you in this case, but as @NyrionDev said, or nil is redundant. You just need to implement your own checks to see if another instance can use that value and the value is not nil.

If you are trying to save the data to a Datastore, you can’t save a Color3 or UDim2, even a Vector, it has to be serialized. You can make it simple and storing it as a table (code below).

local function fromColor3(Color3Value)
	local newColor3Store = {
		R = Color3Value.R * 255,
		G = Color3Value.G * 255,
		B = Color3Value.B * 255,
	}
	return newColor3Store
end

local function fromUDim2(UDim2Value)
	local newUDim2Value = {
		ScaleX = UDim2Value.X.Scale,
		OffsetX = UDim2Value.X.Offset,
		ScaleY = UDim2Value.Y.Scale,
		OffsetY = UDim2Value.Y.Offset,
	}
	return newUDim2Value
end

local function ReturnAllProperties(TheInstance)
	local AllowedProperties = {
		Size = fromUDim2(TheInstance.Size),
		Position = fromUDim2(TheInstance.Position),
		BackgroundTransparency = tonumber(TheInstance.BackgroundTransparency),
		Rotation = tonumber(TheInstance.Rotation),
		BackgroundColor3 = fromColor3(TheInstance.BackgroundColor3),
		BorderColor3 = fromColor3(TheInstance.BackgroundColor3),
		TextScaled = TheInstance.TextScaled,
		Font = TheInstance.Font.Name,
		Text = tostring(TheInstance.Text),
	}

	return AllowedProperties
end

To return the table to a Color3.fromRGB() or a UDim2.new(), use the functions below:

local function toColor3(Color3Table)
	return Color3.fromRGB(Color3Table.R, Color3Table.G, Color3Table.B)
end

local function toUDim2(UDim2Table)
	return UDim2.new(UDim2Table.ScaleX, UDim2Table.OffsetX, UDim2Table.ScaleY, UDim2Table.OffsetY)
end
1 Like

I appreciate the support, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.