--Stickmasterluke


sp=script.Parent
framerate=30

frames=1
running=false
direction=1


function waitForChild(instance,name)
	while not instance:FindFirstChild(name) do
		instance.ChildAdded:wait()
	end
end

waitForChild(sp,"Bottom")
waitForChild(sp.Bottom,"OpenDoor")
waitForChild(sp.Bottom,"Configuration")


function checkframes()
	if sp:FindFirstChild("Bottom") then
		local config=sp.Bottom:FindFirstChild("Configuration")
		if config then
			frames=math.floor(config.TimeToMove.Value*framerate)+1
		end
	end
end
sp.Bottom.Configuration.TimeToMove.Changed:connect(checkframes)
checkframes()


function updatedoor(frame)
	local percentage=frame/frames
	if sp:FindFirstChild("Bottom") then
		local hingedist=1.9
		local hinge1=sp.Bottom:FindFirstChild("Door1")
		if hinge1~=nil then
			hinge1.C1=CFrame.new(-hingedist,-hingedist,0)*CFrame.Angles(0,0,(math.pi/2)*percentage)*CFrame.new(hingedist,hingedist,0)
		end
		local hinge1=sp.Bottom:FindFirstChild("Door2")
		if hinge1~=nil then
			hinge1.C1=CFrame.new(-hingedist,hingedist,0)*CFrame.Angles(0,0,(math.pi/2)*percentage)*CFrame.new(hingedist,-hingedist,0)
		end
		local hinge1=sp.Bottom:FindFirstChild("Door3")
		if hinge1~=nil then
			hinge1.C1=CFrame.new(hingedist,hingedist,0)*CFrame.Angles(0,0,(math.pi/2)*percentage)*CFrame.new(-hingedist,-hingedist,0)
		end
		local hinge1=sp.Bottom:FindFirstChild("Door4")
		if hinge1~=nil then
			hinge1.C1=CFrame.new(hingedist,-hingedist,0)*CFrame.Angles(0,0,(math.pi/2)*percentage)*CFrame.new(-hingedist,hingedist,0)
		end
	end
end

function runloop()
	if not running then
		running=true
		while true do
			if sp:FindFirstChild("Bottom") then
				local frame=sp.Bottom:FindFirstChild("Frame")
				if frame then
					frame.Value=frame.Value+direction
					if frame.Value>frames then
						frame.Value=frames
					end
					if frame.Value<=0 then
						frame.Value=0
					end
					updatedoor(frame.Value)
					wait(1/framerate)
					if frame then
						if (frame.Value<=0 and direction==-1) or (frame.Value>=frames and direction==1) then
							break
						end
					end
				else
					break
				end
			else
				break
			end
		end
		running=false
	end
end

function check(val)
	if val>.5 then
		direction=1
	else
		direction=-1
	end
	runloop()
end

sp.Bottom.OpenDoor.SourceValueChanged:connect(check)
check(sp.Bottom.OpenDoor:GetCurrentValue())



