Help with moving part

i want to make when player touches the part, then the part will go slowly down and when player end the touch it will go back to original position. for some reason my part is glitching up and down and i don’t know what to do please help me.

code:

local movingPart = script.Parent --replace with the part you want to move
local originalY = movingPart.Position.Y
local Debounce = false
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)

local function movePart(targetY)
	local targetPosition = Vector3.new(movingPart.Position.X, targetY, movingPart.Position.Z)
	local tween = game:GetService("TweenService"):Create(movingPart, tweenInfo, {Position = targetPosition})
	tween:Play()
end

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if Debounce == false then
			Debounce = true
			movePart(originalY - 0.25) -- move the part to the new Y position when the button is touched
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		Debounce = false
		movePart(originalY) -- move the part back to the original Y position when the button touch ends
	end
end)

video of the problem:
https://gyazo.com/c82fd70c6c5850e9ce9f477c53bbe12a

I had a similar problem to this, what I did was make a transparent, non-collideable part, called hitbox, then when this part was pressed, I would move the non-transparent, collideable part.

Here is some code:

--workspace
> group
  > movingPart 
  > hitbox
  > script
local movingPart = script.Parent.movingPart
local hitbox = script.Parent.hitbox
local originalY = movingPart.Position.Y
local Debounce = false
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Linear)

local function movePart(targetY)
	local targetPosition = Vector3.new(movingPart.Position.X, targetY, movingPart.Position.Z)
	local tween = game:GetService("TweenService"):Create(movingPart, tweenInfo, {Position = targetPosition})
	tween:Play()
end

hitbox.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if Debounce == false then
			Debounce = true
			movePart(originalY - 0.25) -- move the part to the new Y position when the button is touched
		end
	end
end)

hitbox.TouchEnded:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		Debounce = false
		movePart(originalY) -- move the part back to the original Y position when the button touch ends
	end
end)

yes thank you it worked as you said

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