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

Expected UDim, got number?

I'm not 100% sure what I've done wrong here, but I can tell from the output what I've somewhat done. (in the title)

The error is on line 11.

local count = 1
local modes = {"REDACTED", "REDACTED"}
script.Parent.MouseButton1Click:connect(function()
	for i,v in pairs(modes) do
		local buttonCoords = script.Parent.Position
	    local a = script.Parent:Clone()
	    a.Parent = script.Parent.Parent
	    a.Text = v
	    a.MouseButton1Click:connect(function()
	        script.Parent.Text = v
	    end)
	    a.Position = UDim2.new(buttonCoords.X, 0, buttonCoords.Y + count * 155, 0)
	    count = count + 1
    end
end)

It's supposed to me some sort of drop-down menu. Can someone please help me? Thanks.

1 comment

0

welp no actually its from line 12

1 answer

Ok, so you see, a GUI's position works this way: It has 2 dimensions, x and y. And each dimension is split to 2 things, an offset and a scale. There's an x offset and an x scale, and there's a y offset and a y scale. That's why a Udim2value has for components. And for your information, scale is a percentage, a percentage according to the screen, making it 1 will make it go all the way to the end of the screen, so you'd have to play with the numbers between 0 and 1, it's for easy and quick ui positioning, offset though, is the amount of pixels, it's for more detailed positioning.


And keep in mind that the 1st and 3rd components are the scale and the 2nd and 4th are offset.

Now, after understanding that, what you did wrong is this

UDim2.new(buttonCoords.X, 0, buttonCoords.Y + count * 155, 0)

buttonCoords is a position, and you prety much did this, position.X, that would give you 2 numbers, not just one; and that's where the error came from. That will give you the offset and scale of X, that's why, it told you Udim2 expected got number, because it expected 1 number there, you gave it 2. So you'd have to do this. You are able to do position.X.Scale or position.X.Offset. And that applies for the Y too.

UDim2.new(buttonCoords.X.Scale, 0, buttonCoords.Y.Scale + count * 155, 0)

And that's really it.

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