Part doesn't seem to move down when I clik the mouse button a 2nd time

I am trying to make a part move up when you press the part once and then make it move back down when you click it again.

But obviously when I click it for a second time nothing happens.

I’ve tried all sorts of tutorials and none of the worked and I ended up with this code from all the tutorials.

Code
local part = script.Parent
local _X = part.Position.X
local _Y = part.Position.Y
local _Z = part.Position.Z

local ClickDetector = part.ClickDetector

local HoverBox = part.SelectionBox

local TweenService = game:GetService("TweenService")

--Hover
local function onHover()
	HoverBox.Transparency = 0
end
local function onUnHover()
	HoverBox.Transparency = 1
end
ClickDetector.MouseHoverEnter:Connect(onHover)
ClickDetector.MouseHoverLeave:Connect(onUnHover)

--Click
local function onClick()
	local Clicked = false
	local partTweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)

	local tweenMoveUp = TweenService:Create(part, partTweenInfo, {Position = Vector3.new(_X, _Y + 5, _Z)})
	local tweenMoveDown = TweenService:Create(part, partTweenInfo, {Position = Vector3.new(_X, _Y - 5, _Z)})

	local debounce, isUp

	if debounce then
		return
	end	
	debounce = true

	if not isUp then
		tweenMoveUp:Play()
		isUp = true
	else
		tweenMoveDown:Play()
		isUp = false	
	end

	wait(0.5)
	debounce = false
end
ClickDetector.MouseClick:Connect(onClick)

Any help would be appreciated.

Let’s try this code, enjoy ^^

local TweenService = game:GetService("TweenService")

local Part = script.Parent
local Click = Part:FindFirstChild("ClickDetector")
local Box = Part:FindFirstChild("SelectionBox")
local Clicked = false	

if Click ~= nil and Box ~= nil then

	Click.MouseHoverEnter:Connect(function()
		Box.Transparency = 0
	end)

	Click.MouseHoverLeave:Connect(function()
		Box.Transparency = 1
	end)

	Click.MouseClick:Connect(function()
		local TweenInfos = TweenInfo.new(0.5, Enum.EasingStyle.Back, Enum.EasingDirection.Out, 0, false, 0)
		local Goal = {}	

		if Clicked == false then
			Clicked = true
			Goal.Position = Vector3.new(Part.Position.X, Part.Position.Y + 5, Part.Position.Z)
			local TweenUp = TweenService:Create(Part, TweenInfos, Goal)
			TweenUp:Play()
		elseif Clicked == true then
			Clicked = false
			Goal.Position = Vector3.new(Part.Position.X, Part.Position.Y - 5, Part.Position.Z)
			local TweenDown = TweenService:Create(Part, TweenInfos, Goal)
			TweenDown:Play()
		end

		wait(0.5)
	end)
end
1 Like