Working with CFrames

Hello, I am trying to create a door that will go up when a Key touches it, but I don’t know how to make it go smooth.

I tried using wait(0.1) to make it go up, but it is really buggy.
My script:

local waitTime = Instance.new("BoolValue") -- creates a value if you can open the door.
waitTime = false -- set to false so they can open the door.
script.Parent.Touched:Connect(function(hit) -- when the key touches the door.
if hit.Parent.Name == "Key" and waitTime == false then -- if the key touched it and the boolValue is false then it will go through.
waitTime = true
script.Parent.CFrame = CFrame.new(0,5.1,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.2,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.3,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.4,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.5,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.6,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.7,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.8,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,5.9,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.1,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.2,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.3,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.4,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.5,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.6,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.7,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.8,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,6.9,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.1,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.2,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.3,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.4,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.5,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.6,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.7,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.8,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,7.9,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.1,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.2,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.3,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.4,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.5,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.6,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.7,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.8,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,8.9,0)
wait(0.1)
script.Parent.CFrame = CFrame.new(0,9,0)
else
wait(10)
waitTime = false
script.Parent.CFrame = CFrame.new(0,0,0)
end end)

I searched in the Developer Hub on how can I create a smooth CFrame movement and also in other topics but they’re not very helpful.

2 Likes

You could make your code a lot cleaner and way better if you use the for i = loop, for more information about this loop please proceed to go to the following link which leads to the Roblox Developer API Reference page of the for i = loop: Roblox Developer API Reference - for i = loop

1 Like

@Happy_Errors are you trying to make one of those garage doors, but instead of a Garage door, a House/Shelter door? If your trying that, I would that this would be a better replacement for your thing.

instead of “wait(10)” try replacing it with wait(0.1)? (on the 4th to last line)

  • If it doesn’t work do what Nerkonl said.

It would be better to use TweenService instead of loops(for this purpose):

local tweenService = game:GetService('TweenService')
local tweenInfo = TweenInfo.new(1) --TweenInfo that lasts 1 second
local properties = {CFrame = CFrame.new(0,9,0)}
local tween = tweenService:Create(script.Parent, tweenInfo, properties) --Creating tween
tween:Play() --Play tween
2 Likes

The reason why I’m suggesting TweenService is because it’s smoother than a for loop with wait() could ever be, and it’s also more versatile.

2 Likes