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

Help with fixing problem involving :TweenPosition()?

So, I have a Menu Gui basically with several buttons on the menu. What I want to happen is, I want the buttons to slide out when the mouse hovers over it, and go back in place when the mouse leaves the button.

However, as I have several buttons, I don't want to input the button Position for each button. I attempted to create a function to solve this, and it somewhat works. However, when it happens multiple times it starts moving off the intended location.

Here's what happens: http://i.imgur.com/aYwrnD7.gifv

If you could, please explain to me what the problem is and what I can do to fix it?

--//Variables
local teleportService = game:GetService("TeleportService")
local players = game:GetService("Players")
local plr = players.LocalPlayer
local trainingPlace = nil
local rallyPlace = nil
local rclPlace = nil
local trainButton = script.Parent:WaitForChild("trainButton")
local rallyButton = script.Parent:WaitForChild("rallyButton")
local dbButton = script.Parent:WaitForChild("dataBaseButton")
local raidButton = script.Parent:WaitForChild("raidButton")

function moveIn(button)

	local pos = button.Position
	local oldpos = button.Position
	local newpos = pos + UDim2.new(.05,0,0,0)
	button:TweenPosition( newpos, "Out", "Quad", .2, true)

end

function moveOut(button)

	local pos = button.Position
	local oldpos = button.Position
	local newpos = pos - UDim2.new(.05,0,0,0)
	button:TweenPosition( newpos, "In", "Quad", .2, true)

end

trainButton.MouseEnter:connect(function()
	moveIn(trainButton)
end)

trainButton.MouseLeave:connect(function()
	moveOut(trainButton)
end)

1 answer

✓ Accepted answer

The problem is that you are adding on to the current position when tweening.

Instead, tween to predefined locations. At the top of the script, set local defaultPosition = button.Position and local hoverPosition = defaultPosition + UDim2.new(0.5, 0, 0, 0) and then tween to defaultPosition in moveOut and hoverPosition in moveIn

2 comments

0

Aye, this works, but I have multiple buttons. I was trying to avoid writing variables for all the button's position and have it done in a function .

0

That is why it is based on the buttons default position. The script should be reusable, either with completely new scripts or in a for loop.

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