Search code examples
luaroblox

Why isnt the position tweening?


I am fairly new to Lua, and I am trying to make a game in Roblox. I am currently working on an open and close button on my Miner GUI.

Code

local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
local Opened = false
if Opened == false then
    print('Gui Is Closed')
    Opened = true
end
if Opened == true then 
    print('Gui Is Opened')
end
script.Parent.Button.MouseButton1Click:connect(function()
    GUI:TweenPosition(UDim2.new(1, 0, 1, 0),'Bounce',1.5)
    
    
end)

I want the GUI to disappear and reappear

Game


Solution

  • The GUIObject:TweenPosition function has a few parameters. Some have defaults, but if you want to override them, you need to override them in the right order. Your example looks like it is missing the easingDirection argument.

    Also, you need to call TweenPosition on the object you want to animate. In your example, it will be the variable Frame.

    -- define some variables and grab some UI elements relative to the script's location
    local Button = script.Parent.Button
    local Frame = script.Parent.Parent.Parent.Parent.Parent.MinerGuiManager.MinerFrame
    local Opened = false
    
    Button.MouseButton1Click:connect(function()
        local TargetPos
        if Opened then
            -- move the frame offscreen to the lower right
            -- NOTE - once we move it offscreen, we won't be able to click the button
            --        and bring it back onscreen... (change this number later)
            TargetPos = UDim2.new(1, 0, 1, 0)
        else
            -- move the frame to the center of the screen
            local frameWidthOffset = Frame.Size.X.Offset * -0.5
            local frameHeightOffset = Frame.Size.Y.Offset * -0.5
            TargetPos = UDim2.new(0.5, frameWidthOffset, 0.5, frameHeightOffset)
        end
    
        -- animate the frame to the target position over 1.5 seconds
        local EaseDir = Enum.EasingDirection.Out
        local EaseStyle = Enum.EasingStyle.Bounce
        Frame:TweenPosition(TargetPos, EaseDir, EaseStyle, 1.5)
    
        -- toggle the Opened value
        Opened = not Opened
    end)