In my game there is a little menubutton on the left hand side of the screen, I want the menu button kinda tucked away but when a user puts his mouse over it it will slide out and show the full button and when he removes his mouse it will slide back. Basically when you put the mouse over it right now it just keeps sliding and will keep sliding every time you put your mouse over it until it goes off screen (also it wont go back because I have no clue how to add that) All I need is it to stop at a certain point (doesn't matter where you put it just point out that's what it is) and go back after removing the mouse for about 3 seconds.
Here is the code I have so far (this only includes the sliding forwards, I have no clue what I'll do about sliding back)
Menu = script.Parent MenuButton = Menu.MenuButton MenuPos = MenuButton.Position MenuX = MenuPos.X.Scale print("Pos of menuX is " .. MenuX) if MenuX <= 0.5 then function OverMenuButton() for MenuPosition = 1, 20 do if MenuPosition <= 20 then MenuButton.Position = MenuButton.Position +UDim2.new(0.01, 0, 0, 0) end wait() end end end MenuButton.MouseEnter:connect(OverMenuButton)
If you guys could help, and give wiki links that would be great!
A simple way to do this is to maintain a current x
position of the menu button.
When the mouse is over, we add to x
, when it's not, we remove from x
.
mouseOver = false x = 0 MenuButton.MouseEnter:connect(function() mouseOver = true end) MenuButton.MouseLeave:connect(function() mouseOver = false end) while wait() do if mouseOver then x = x + 0.01 else x = x - 0.01 end -- limit `x` to be between 0 and 0.5: x = math.min( 0.5 , math.max( 0, x ) ) -- Use `x` to make a position: MenuButton.Position = UDim2.new(x , 0 , 0 , 0) end
(Note this solution in general isn't mobile friendly; also that linear motion isn't very comfortable to watch but will work)
As per Perci's comment, the tween method is a good way to do this. The motion won't be linear, and it doesn't allow quite as much of a degree of control, but everything is handled by the Roblox classes.
currentYPos = menuButton.Position.y.scale MenuButton.MouseEnter:connect(function() menuButton:TweenPosition(UDim2.new(0.5, 0, currentYPos, 0), "Out", "Quad", 1, true) end) MenuButton.MouseLeave:connect(function() menuButton:TweenPosition(UDim2.new(0, 0, currentYPos, 0), "Out", "Quad", 1, true) end)
See this link: http://wiki.roblox.com/index.php?title=TweenPosition