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

How to add a udim2 value to another udim2 value?

Hello everyone, so I'm doing a thing with a GUI and I want to create a cool effect that moves a bunch of buttons inside a folder to the left each other, but the value always comes {0,0,0,0}

Here's the script:

for i,v in pairs (script.Parent.Parent:GetChildren()) do
			local Position = UDim2.new(v.Position + UDim2.new(-0.7,0,0,0))--I tried with - and * and didn't work
			v:TweenPosition(UDim2.new(Position),"Out","Quad",1)
			print(Position)--Prints {0,0},{0,0}
			print(v.Position) --+Prints high numbers such as  {0.0599999987, 0}, {0.649999976, 0} for some reason, shouldn't even be posible.
			wait(.3)
		end

Huge thanks to whoever helps me!

4 comments

0

I might be wrong on this but I'm sure tweening happens asynchronously i.e. when you print v.Position the tweening has just started, so the output you're getting is close to where v is starting off at, not ending.

0

Even when I remove the tween line the same numbers appear.

0

Yeah, that's v's original coordinates. 0.06,0.65 are reasonable scale coordinates, so nothing there is really out of place. Do you see the gui's position visually change when you tween it?

0

Yes. They all move to the "Position" value, which is {0,0},{0,0}

1 answer

✓ Accepted answer

This is because the UDim2.new constructor expects 2 or 4 arguments, while you're just providing one, which will make it return "{0,0}, {0,0}".

To fix your script, you just have to remove the UDim2.new in the beginning, so it should look like this now:

for i,v in pairs (script.Parent.Parent:GetChildren()) do
		
		-- Fixing the position
		local Position = (v.Position + UDim2.new(-0.7,0,0,0))
		
		-- Tweening the button
		v:TweenPosition(Position, "Out", "Quad", 1);
		-- Waiting :o
		wait(.3)
end

1 comment

0

Wow it was that simple? Thanks a bunch mate.

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