How can i make a script when u click a button part the door moves to a minimun position

Hello guys i was asking that when u click a button(part)it moves the door moves to like a elevator and it at least stop for 5 seconds and it comes back it originally position

Heres the script!

local ElevatorDoor = game.Workspace.ElevatorDoor
local Button = script.Parent

Button.ClickDetector.MouseClick:Connect(function()
while true do
ElevatorDoor.Position = ElevatorDoor.Position + Vector3.new(0.1,0,0)
wait(0.5)
end
end)

Thx for reading i hope see solutions very soon!

I seriously recommend tweening! It’s super fun and easy to get into! Here is the API for it: Tween | Roblox Creator Documentation There’s also a ton of tutorials on it over at youtube or some other place, Good luck with your door!

1 Like

You’re trying to move an object after Click detection, what your code currently does is commence a thread with a loop which is infinite to move it towards X-axis each 0.5s

What you preferably want to do in this case is to commence an operation that moves the object and reverses without infinite looping or leave opportunity for interruption, you can do that by adding a debounce, and instead of looping the position use an efficient way to move it like Tweening

TweenInfo has parameters that allow for reversing visuals and delaying them which fits what you want to achieve here

local debounce = true
local tween

MouseClick:Connect(function()
      if debounce == true then -- check if it's not already in action
           debounce = true -- put it in action
           tween:Play() -- do the deed
           tween.Completed:Wait()
           debounce = false -- disable the condition blocking the action
     end
end)

Here is a documentation for more information: TweenService

Thank u! im really grateful that u helped me