Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with fixing problem involving :TweenPosition()?

Asked by 7 years ago

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

Log in to vote
1
Answered by 7 years ago

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

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 . SketchTitan 40 — 7y
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. SwardGames 325 — 7y
Ad

Answer this question