How can I fix the else?

I never really knew how to use the elseif or else, the rest yes but this I do not know

local Presionar = script.Parent
local Frame = script.Parent:WaitForChild("Visible"):WaitForChild("Botones")

local PositionOpen = UDim2.new(0, 0,0 ,0)
local PositionClose = UDim2.new(0, 0,-1 ,0)

Presionar.MouseButton1Click:Connect(function()
	if Frame.Position == TweenInfo then
		Frame:TweenPosition(PositionOpen)
	else
		Frame:TweenPosition(PositionClose)
	end
end)

Don’t you mean PositionClose or PositionOpen…? Also elseif will just pass on the next possible conditional check is the first if statement hasn’t met the correct requirements

else is basically just if nothing has met the right requirements, you can use that statement

Try use debounce
Like that :

local open = false 
Presionar.MouseButton1Click:Connect(function()
     if open  then 
          open= true
          Frame:TweenPosition(Openframe)
     else
         open = false 
         Frame:TweenPosition(Closeframe)
     end
end)
local Presionar = script.Parent
local Frame = script.Parent:WaitForChild("Visible"):WaitForChild("Botones")


local PositionOpen = UDim2.new(0, 0, 0, 0)
local PositionClose = UDim2.new(0, 0, -1, 0)


Presionar.MouseButton1Click:Connect(function()
	Frame:TweenPosition((Frame.Position == PositionOpen) and PositionClose or PositionOpen)
end)

With override:

local Presionar = script.Parent
local Frame = script.Parent:WaitForChild("Visible"):WaitForChild("Botones")


local PositionOpen = UDim2.new(0, 0, 0, 0)
local PositionClose = UDim2.new(0, 0, -1, 0)


Presionar.MouseButton1Click:Connect(function()
	Frame:TweenPosition(
		(Frame.Position == PositionOpen) and PositionClose or PositionOpen, -- endPosition
		nil, -- EasingDirection
		nil, -- EasingStyle
		nil, -- time
		true, -- override
		nil -- callback
		)
end)

Dbg TweenObj:

local Presionar = script.Parent
local Frame = script.Parent:WaitForChild("Visible"):WaitForChild("Botones")


local PositionOpen = UDim2.new(0, 0, 0, 0)
local PositionClose = UDim2.new(0, 0, -1, 0)


local LastTween = nil
local Open = (Frame.Position == PositionOpen)
Presionar.MouseButton1Click:Connect(function()
	if LastTween ~= nil then LastTween:Destroy() end
	LastTween = game:GetService("TweenService"):Create(Frame, TweenInfo.new(1), {Position = Open and PositionClose or PositionOpen})
	Open = not Open
	LastTween:Play()
end)

Dbg Override:

local Presionar = script.Parent
local Frame = script.Parent:WaitForChild("Visible"):WaitForChild("Botones")


local PositionOpen = UDim2.new(0, 0, 0, 0)
local PositionClose = UDim2.new(0, 0, -1, 0)


local Open = (Frame.Position == PositionOpen)
Presionar.MouseButton1Click:Connect(function()
	Frame:TweenPosition(
		Open and PositionClose or PositionOpen, -- endPosition
		nil, -- EasingDirection
		nil, -- EasingStyle
		nil, -- time
		true, -- override
		nil -- callback
	)
	Open = not Open
end)

Your main error is that TweenInfo is not defined anywhere in the provided code snippet. Changing it to something like PositionClose would resolve that issue

However, the easiest way to do this is to use a debounce, instead of checking the position arbitrarily. This also guarantees that clicking mid-animation works consistently.

local Presionar = script.Parent
local Frame = script.Parent:WaitForChild("Visible"):WaitForChild("Botones")

local PositionOpen = UDim2.new(0, 0, 0, 0)
local PositionClose = UDim2.new(0, 0, -1, 0)

local isOpen = false --Initial state of closed

Presionar.MouseButton1Click:Connect(function()
    isOpen = not isOpen --Invert isOpen, so if its open it now isn't, and if it was closed it is now open
	if isOpen then
        --Tween to open if open
		Frame:TweenPosition(PositionOpen)
	else
        --Otherwise, tween to closed
		Frame:TweenPosition(PositionClose)
	end
end)

cc @MahDev0 Your implementation won’t work because open will already be true when you set it to true.

2 Likes