How would I make a tween add to a position

I’m trying to make a button move when hovered over, and this is my code so far: (I got a lot of help from StrongBigeMan9)


local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script

local function onHover(item)
	local currentpos = item.Position
	--[[item:TweenPosition(
		UDim2.new(200,0,200,0),-- end size
		"Out", -- easing direction
		"Quad", -- easing style
		1.5, --time in seconds
		false --override?
		--tween2
	)]]--
end

for _, item in ipairs(GuiLayer) do
	if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
		item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
			onHover(item)
		end)
	end
end

Since the TextButtons are in different positions, tweening them to the same position obviously wouldn’t work. So, how would I make it (the button) so that it adds on stuff (moves forward) on the X axis?

I want it to be like when you are adding Vector3 to a part:

 something + Vector3.new(5,0,0)

Thanks!

Something like this?

I want it to tween the position, not the size, so that the button moves forward. I’m also not really getting your code, can you explain it?

So you basically want it to change its position when mouse hovering over it, if so then I got the perfect script for you

You can reference its current position when adjusting the parameters of the TweenPosition and increment it a certain amount:

local function onHover(item)
	local currentpos = item.Position
	item:TweenPosition(
		UDim2.new(currentpos.Scale.X + number, 0, currentpos.Scale.Y + number, 0)
        -- This will take its current X & Y Scale for Position and add it by number
    )
end

Would I have to add anything else to that code? It’s giving me the error

Scale is not a valid member of UDim2

Oh woops, I was indexing Position.Scale when the axis needs to be referenced first:

UDim2.new(currentpos.X.Scale + number, 0, currentpos.Y.Scale + number, 0)

I’m trying to make it so the button moves back when your mouse leaves, but it doesn’t work very well, could you help me out again? Thanks!

Code:


local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script

local function onHover(item)
	local currentpos = item.Position
	item:TweenPosition(
		UDim2.new(currentpos.X.Scale + 0.03, 0, currentpos.Y.Scale + 0, 0)
	)
end

local function onExit(item)
	local currentpos = item.Position
	item:TweenPosition(
		UDim2.new(currentpos.X.Scale + -0.03, 0, currentpos.Y.Scale + 0, 0)
	)
end

for _, item in ipairs(GuiLayer) do
	if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
		item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
			onHover(item)
		end)
		item.MouseLeave:Connect(function()
			onExit(item)
		end)
	end
end

(I’m using multiple buttons)

You can reproduce this the same way you would do with a Vector3 value but instead just a UDim2 position.

Adding the other properties back in might allow it to be smoother, if that’s what you’re referring to:

What do you mean? (how would I add them back)
When the mouse leaves, it just doesn’t move back at all sometimes.

I was referring to the other parameters of the TweenPosition method – after the end goal, there’s EasingStyle/Direction, duration of the tween, etc.

As far as it not tweening back goes, try adding some print statements inside of the functions to see when it’s being activated.


I’m tired atm so I might need to come back to this later when everything is fresh, haha.

1 Like

Alright, if you’re back (I gotta go soon) here’s the code that isn’t working:


local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script

local function onHover(item)
	local currentpos = item.Position
	item:TweenPosition(
		UDim2.new(currentpos.X.Scale + 0.03, 0, currentpos.Y.Scale + 0, 0)
		"Out", -- easing direction
		"Quad", -- easing style
		0.1, --time in seconds
		false --override?
	)
end

local function onExit(item)
	local currentpos = item.Position
	item:TweenPosition(
		UDim2.new(currentpos.X.Scale + -0.03, 0, currentpos.Y.Scale + 0, 0)
		"Out", -- easing direction
		"Quad", -- easing style
		0.1, --time in seconds
		false --override?
	)
end

for _, item in ipairs(GuiLayer) do
	if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
		item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
			onHover(item)
		end)
		item.MouseLeave:Connect(function()
			onExit(item)
		end)
	end
end

Error: layers.RipPBB_TUD.PlayerGui.LoadingScreen.LoadingScreen4.TweenScript:18: attempt to call a UDim2 value - Client - TweenScript:18

I also added some print functions, and it looks like both are running, but there’s the error.

I have no idea what’s wrong, so I can’t really fix it. I also don’t get what was wrong with my original code without the other parameters of the TweenPosition method.

Edit: I removed the parameters which were causing the error, and it seems like one is running right after the other making it weird. Would I make it override or something? No idea how to fix this too.

Thanks!

I think it’s cause you forgot to put a comma , after making the UDim2s in the functions, also, Iwould recommed making the override true so the tween can be changed mid tween

1 Like

So I have multiple buttons, and last problem is, when the player hovers over button A, and goes to button B, this happens:

Hover over button A:
Button A moves forward
Goes to button B
Button B moves back, when it never moved forward.

Do you know how I would fix that? Would I have to make separate scripts for all the buttons? Thanks.

Button A does move backwards, but button B which never moved forward also moved backwards.

May I see your current code so far after the changes?

Code:


local GuiLayer = script.Parent:GetChildren() -- Gets everything on the same layer as the script

local function onHover(item)
	local currentpos = item.Position
	print("run")
	item:TweenPosition(
		UDim2.new(currentpos.X.Scale + 0.02, 0, currentpos.Y.Scale + 0, 0),
		"Out", -- easing direction
		"Quad", -- easing style
		0.2, --time in seconds
		true --override?
	)
end

local function onExit(item)
	local currentpos = item.Position
	print("run")
	item:TweenPosition(
		UDim2.new(currentpos.X.Scale + -0.03, 0, currentpos.Y.Scale + 0, 0),
		"Out", -- easing direction
		"Quad", -- easing style
		0.1, --time in seconds
		true --override?
	)
end

for _, item in ipairs(GuiLayer) do
	if item:IsA("TextButton") then -- Checks if the Item is a TextButton, for example
		item.MouseEnter:Connect(function() -- Activates the function called "onHover" whenever any of those items are hovered over
			onHover(item)
		end)
		item.MouseLeave:Connect(function()
			onExit(item)
		end)
	end
end

Also wait, are you able to show a reproduction of the issue you’re facing? Maybe try try setting the overrides to false since the time is extremely small?

Here’s there repro:
image

Normal:
image

I’ll try setting the overrides in a bit.