Laggy door animation

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.

2 Likes

I would recommend looking into TweenService. You can run a tween like this

local TS = game:GetService("TweenService")

local info = TweenInfo.new(5, Enum.EasingStyle.Linear) -- 5 is the seconds the tween lasts
local goal = {Position = Vector3.new(0,10,0)} -- set all the properties you want

local tween = TS:Create(workspace.Baseplate, info, goal) 
-- put the instance whose properties you want to be animated as the first argument
tween:Play()
tween.Completed:Wait()
print("Tween finished")

I’ve tried TweenService for this, but I really don’t think I can add it in to the script. The handle and door rotate around a hinge, which is the PrimaryPart of the models for the hinge and the door, and when the PrimaryPart is rotated the whole model is rotated, which (I’m pretty sure) isn’t possible with TweenService. Even if I find a way to rotate the models around a hinge, I’ll have to rotate every part and union separately which would make the code even more cumbersome. Maybe there’s some other way to solve this, but I’m not sure. I think I’ll just use animations, at this point it’s my best option unfortunately

Why don’t you just utilize animation controllers and animate the door separately and run the animation whenever needed?

1 Like

I am trying to currently do that right now. I don’t really know how animators work in Roblox, but I know I’ve had a lot of trouble with them already. Its weirdly over complicated which is why I didn’t want to use them in the first place, but I have to now. Was worth a shot with the “for i,” loops.

I apologize I haven’t read this message properly but there is a way utilizing Motor6Ds to animate the model as a whole, and I am pretty sure there’s a good video covering that as well!

Here you go:

1 Like

Thanks for the help, I’ll keep Motor6Ds in mind for the future. I should continue working on the everything else in the game and stop working on a door for 13 hours before I lose motivation (or sanity, which ever comes first). Still trying to figure it out, but I’ll manage. Just hope I actually finish my game this time so all the fun times spent with the door don’t go to waste…

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