In my roblox game, I made an animated door, with an animated handle and latch. It works well, even though the code is a little cumbersome, it gets the job done. But the “for i,” loops I am using to animate the door, handle and latch, they’re not good because for the animation to be smooth, it has to be very slow, but I need these animations to be at the speed they are. Is there any way to do this without using actual animations because I hate dealing with those? Here is the script.
local Hinge = script.Parent.PrimaryPart
local opened = false
local n = math.random(38, 48) -- 38 and 48
local Handle = script.Parent.Handle
local Latch = script.Parent.Latch
local n2 = math.random(50, 70)
function HandlePress()
script.Parent.SoundPART.open:Play()
for i = 1, 10 do -- Handle press
Handle:SetPrimaryPartCFrame(script.Parent.Handle.Part.CFrame*CFrame.Angles(math.rad(-2), 0, 0))
Latch.Position -= Vector3.new(0, 0, 0.01) -- Latch backwards
wait()
end
end
function HandlePressClose()
script.Parent.SoundPART.open:Play()
for i = 1, 10 do -- Handle press
Handle:SetPrimaryPartCFrame(script.Parent.Handle.Part.CFrame*CFrame.Angles(math.rad(-2), 0, 0))
Latch.Position -= Vector3.new(0.01, 0, 0) -- Latch backwards
wait()
end
end
function HandleRelease()
for i = 1, 10 do -- Handle release
Handle:SetPrimaryPartCFrame(script.Parent.Handle.Part.CFrame * CFrame.Angles(math.rad(2), 0, 0))
Latch.Position = Latch.Position + Vector3.new(0.01, 0, 0) -- Latch forwards
wait()
end
end
function HandleReleaseClose()
for i = 1, 10 do -- Handle release
Handle:SetPrimaryPartCFrame(script.Parent.Handle.Part.CFrame * CFrame.Angles(math.rad(2), 0, 0))
Latch.Position = Latch.Position + Vector3.new(0, 0, 0.01) -- Latch forwards
wait()
end
end
function OpenDoor()
if opened == false then
opened = true
HandlePress()
wait(0.18)
for i = 1, 1.25 * n do -- Door open
script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(2), 0))
wait()
end
wait(0.2)
HandleRelease()
wait(n2) -- Wait for the door to be open for a random duration between 50 and 70 seconds
CloseDoor()
else
HandlePressClose()
CloseDoor()
HandleReleaseClose()
end
end
function CloseDoor()
opened = false
for i = 1, 1.25 * n do -- Door close
script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-2), 0))
wait()
end
script.Parent.SoundPART.close:Play()
end
script.Parent.ClickPart.ClickDetector.MouseClick:Connect(OpenDoor)
I appreciate any help.