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)
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.
--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)