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

Why doesn't the GUI go to the position in the tween?

Asked by 5 years ago
Edited 5 years ago
script.Parent.MouseButton1Click:Connect(function()

    local openThis = script.Parent.Parent.ShopFrame

    if openThis.Visible == true then

        local TweeningService = game:GetService("TweenService")

        local partChanges = {
            Position = UDim2.new({0.3, 0},{1.5, 0})
        }

        local TweenInformation = TweenInfo.new(
            0.2,  -- Tween length
            Enum.EasingStyle.Bounce, -- Easing Style
            Enum.EasingDirection.In, -- Easing Direction
            0, -- Repitition times
            false, -- Reverse?
            0 -- Delay
        )

        local tween = TweeningService:Create(openThis, TweenInformation, partChanges)

        tween:Play()
        tween.Completed:wait()

        openThis.Visible = false

        openThis.Position = UDim2.new({0.3, 0},{-0.8, 0})


    elseif openThis.Visible == false then


        openThis.Visible = true

        local TweeningService = game:GetService("TweenService")

        local partChanges = {
            Position = UDim2.new({0.3, 0},{0.15, 0})
        }

        local TweenInformation = TweenInfo.new(
            0.2,  -- Tween length
            Enum.EasingStyle.Bounce, -- Easing Style
            Enum.EasingDirection.In, -- Easing Direction
            0, -- Repitition times
            false, -- Reverse?
            0 -- Delay
        )

        local tween = TweeningService:Create(openThis, TweenInformation, partChanges)

        tween:Play()
    end
end)

That is the code in a LocalScript, the script is in another GUI button that opens "openThis". First time I click the button, the GUI appears but then tweens to the Position ({0, 0} {0, 0}). When I click the button again, the GUI just disappears without tweening. If I open the GUI again (click the button). It just appears on the same position, no matter how many times I click the button, the GUI stays on the same place.

0
rather than using TweenService, you can use the tweenposition function for guis. theking48989987 2147 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

My assumption is that your problem lies in the fact that you are calling two tables, when UDim2.new() already sets the position to be written out as

UDim2.new(0, 0, 0, 0)

instead of

UDim2.new({0, 0}, {0, 0})

However, with gui instances, there is an easier way to perform the Tween, notably TweenPosition

Using this, I've Revised your script to the one below

local debounce = true

script.Parent.Activated:Connect(function()
if debounce == false then return end
debounce = false
local openThis = script.Parent.Parent.ShopFrame
    if openThis.Visible == true then
        openThis:TweenPosition(UDim2.new(0.3, 0, 1.5, 0), "In", "Bounce", 0.2)
        wait(0.2)
        openThis.Visible = false
        openThis.Position = UDim2.new(0.3, 0, -0.8, 0)
    elseif openThis.Visible == false then
        openThis.Visible = true
        openThis:TweenPosition(UDim2.new(0.3, 0, 0.15, 0), "In", "Bounce", 0.2)
        wait(0.2)
    end
debounce = true
end)

It is also better to use .Activated rather than .MouseButton1Click so that all devices can set off the event.

Edit: I added a debounce so that if the player clicks the button multiple times, it will only fire after the function is completed

Ad

Answer this question