How to make this tween go back after the keybind is pressed?

Like the title suggests. I’ve tried doing an “else” and then a reversal of the first tweening code, but that doesn’t seem to work.

local Player = game.Players.LocalPlayer -- general setup
local GUI = script.Parent.Parent
local MainFrame = script.Parent

local MenuOpen = GUI.Sounds.MenuOpen -- sounds
local MenuClose = GUI.Sounds.MenuOpen

Player:GetMouse().KeyDown:Connect(function(Key)
	if Key == "m" then
	MenuOpen:Play()
		
	MainFrame:TweenPosition(
		UDim2.new(0.463, 0, 0.174, 0), -- end position	
		"Out", -- easing direction
		"Quad", -- easing style
		1, -- time in seconds
		false -- override any other tweens
			
		)
	else
		MainFrame:TweenPosition(
			UDim2.new(0.463, 0, -0.642, 0), -- end position	
			"In", -- easing direction
			"Quad", -- easing style
			1, -- time in seconds
			false -- override any other tweens

		)
		script.Parent.Visible = not script.Parent.Visible
	end
end)

I think you just need another if statement like so:

local Player = game.Players.LocalPlayer -- general setup
local GUI = script.Parent.Parent
local MainFrame = script.Parent

local MenuOpen = GUI.Sounds.MenuOpen -- sounds
local MenuClose = GUI.Sounds.MenuOpen

Player:GetMouse().KeyDown:Connect(function(Key)
	if Key == "m" then
        if not script.Parent.Visible then
	        MenuOpen:Play()
		    script.Parent.Visible = true
	        MainFrame:TweenPosition(
		        UDim2.new(0.463, 0, 0.174, 0), -- end position	
		        "Out", -- easing direction
		        "Quad", -- easing style
		        1, -- time in seconds
		        false -- override any other tweens
			
		        )
	    else
		    MainFrame:TweenPosition(
			    UDim2.new(0.463, 0, -0.642, 0), -- end position	
			    "In", -- easing direction
			    "Quad", -- easing style
			    1, -- time in seconds
			    false -- override any other tweens

		    )
            task.wait(1)
		    script.Parent.Visible = false
        end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.