Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

UDim errors: "Y cannot be assigned to"?

Hey all. I want to constantly keep a GUI's Y Position to 0,0. I tried this script but this error popped up:

Y cannot be assigned to

I understand that there's UDim and UDim2, but in this case UDim2 wouldn't be helpful.

script.Parent.Changed:connect(function(p)
	script.Parent.Position.Y = UDim.new(0,0)
end)

Thanks in advance!

2 answers

The problem is that UDim2 and UDim is that its properties are read only meaning that you need to set a new UDim each time, UDim2 Properties.

You had the correct idea to use the change event, but you also should check what was changed.

Here is a quick example:-

local frm = script.Parent 

script.Parent.Changed:connect(function (propertie)
	if propertie == 'Position' then -- only run when the position is changed
		frm.Position = UDim2.new(frm.Position.X,0) -- a new udim must be set each time
	end
end)

Hope this helps

2 comments

0

Thanks. Now I have another problem. I flipped the brackets and replaced X with Y. The GUI's position is 0,0,0.5,0. When I run and drag the GUI, its position is always 0,0,0,0. Why?

0

The UDim2 constructor takes four parameters, not only two: UDim2.new(xScale, xOffset, yScale, yOffset).

Actually, UDim2.new is the best way to use this in my opinion. The only way you can write to X or Y of a Position or Size, is by doing it within the Udim2.new

Take this script for example:

-- LocalScript in StarterGui

local gui = Instance.new("ScreenGui")
local sp = Instance.new("TextLabel", gui)
sp.Size = UDim2.new(0, 100, 0, 50)
gui.Parent = script.Parent

wait(1)

sp.Position = UDim2.new(sp.Position.X.Scale, sp.Position.X.Offset, 0.5, 0)

wait(1)

sp.Position = UDim2.new(0.5, 0, sp.Position.Y.Scale, sp.Position.Y.Offset)

It maintains the previous position for x, while changing y's Position. Then it does the same for x.

Hope this helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.