Error occurs trying to get rid of offset of the GuI object

Hello. I am trying to make a simple plugin that takes the selection of GuiObjects, and changes their Size and Position values to be based entirely off of Scale rather than offset. However, when I try to run it, it gives me the error “Offset cannot be assigned to - Edit - Script:10”. I have tried looking elsewhere, and I am unsure as to why the error is occurring, as I’m just changing the offset of each UDim in the UDim2 to be 0. Is there a specific method that I should be using instead?

local screenSize = workspace.Camera.ViewportSize
local Selection = game:GetService("Selection")

local toolbar = plugin:CreateToolbar("Offset to Scale Conversion")
local button = toolbar:CreateButton("Convert", "Converts a GUI offset to scale", "rbxassetid://14978048121")
--same image as the one from the tutorial as i dont really care about the way it looks

local function makeUdims(x, y, old)
	local udim = UDim2.new((x/screenSize), 0, (y/screenSize), 0)
	udim = udim+old
	udim.X = UDim.new(udim.X.Scale, 0)
	udim.Y = UDim.new(udim.Y.Scale, 0)
	return udim
end

local function convertOffsetToScale()
	local selectedObjects = Selection:Get()
	if #selectedObjects>0 then
		for i,v in pairs(selectedObjects)  do
			if v:IsA("GuiObject") then
				print("ete")
				local pos = v.Position
				local size = v.Size
				
				pos = makeUdims(pos.X.Offset, pos.Y.Offset, pos)
				size = makeUdims(size.X.Offset, size.Y.Offset, size)
				v.Position = pos
				v.Size = size
			end
		end
	end
	
end

button.Click:Connect(convertOffsetToScale)

This is trying to directly set the X and Y axes of the UDim2 (but as far as I’m aware, it doesn’t let you do that, which is what the error might be implying).

One possible way to fix this could be to overwrite both the X and Y axes at once with a new UDim2 value:

local function makeUdims(x, y, old)
	local udim = UDim2.new((x/screenSize), 0, (y/screenSize), 0)
	udim = udim+old
    udim = UDim2.new(udim.X.Scale, 0, udim.Y.Scale, 0)
	return udim
end

I’m pretty sure that would resolve it, but if it doesn’t, hopefully someone else who’s more knowledgeable about that will correct me.

1 Like

Thanks for replying, but I found out the issue myself. I was being dumb and forgot to save the changes to the plugin using the editor (I’ve never made a plugin before). It works somewhat fine now.
Edit: I also had an issue where I forgot that screenSize contained two values, X and Y. I had to fix that as well.

1 Like

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