-- R6 only!


repeat game:GetService("RunService").Stepped:Wait() until game.Players.LocalPlayer
local player = script.Parent.Parent
local mouse = player:GetMouse()
local selecting = false
local valve

mouse.Button1Up:Connect(function()
	if selecting == true then
		 valve = mouse.Target
		if valve.Using.Value == false and valve.Parent == workspace.Valves and script.Parent.IsUsingValve.Value == false then
			if box then
				box:Destroy()
			end
			valve.Using.Value = true
			script.Parent.IsUsingValve.Value = true
			local camera = workspace.CurrentCamera
			local tweenservice = game:GetService('TweenService')
			local cf = camera.CFrame
			repeat
				wait(.01)
				camera.CameraType = Enum.CameraType.Scriptable
			until camera.CameraType == Enum.CameraType.Scriptable
			character.HumanoidRootPart.Anchored = true
			script.Parent.ValveUI.Enabled = true
			tweenservice:Create(camera,TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out), {CFrame = valve.offset.WorldCFrame}):Play()
			repeat
				wait(.01)
			until valve.Orientation.X > 59
			wait(1.5)
			script.Parent.ValveUI.Enabled = false
			valve.Turn:Play()
			valve.Parent = workspace.Valves.Completed
			valve.Using.Value = false
			wait(0.3)
			tweenservice:Create(camera,TweenInfo.new(1,Enum.EasingStyle.Cubic,Enum.EasingDirection.Out), {CFrame = cf}):Play()
			valve = nil
			wait(1)
			script.Parent.IsUsingValve.Value = false
			character.HumanoidRootPart.Anchored = false
			repeat
				wait(.01)
				camera.CameraType = Enum.CameraType.Custom
			until camera.CameraType == Enum.CameraType.Custom

		end
	end
end)

mouse.KeyDown:Connect(function(key)
	if key == "d" then
		holding = true
		print('play')
		e = 'Front'
		end
end)

mouse.KeyUp:Connect(function(key)
	if key == "d" then
		holding = false
		e = 'nil'
		print('stop')
		
	elseif key == "a" then
		holding = false
		e = 'nil'
		print('stop')

	end
end)

while wait() do	
	
	for i,v in pairs(workspace.Valves.Completed:GetChildren()) do
		if i == 5 then
			workspace.Door.Open:FireServer()
		end
	end
	if valve and holding == true then
		if e == 'Front' then
			print('Q')
			valve.CFrame = valve.CFrame * CFrame.fromEulerAnglesXYZ(0.090,0,0)

		elseif e == 'Back' then
			print('E')
			valve.CFrame = valve.CFrame * CFrame.fromEulerAnglesXYZ(-0.050,0,0)


		end
	end
	
	if mouse.Target then
		if script.Parent.IsUsingValve.Value == false and mouse.Target.Name == '1' or mouse.Target.Name == '2' or mouse.Target.Name == '3' or mouse.Target.Name == '4' or mouse.Target.Name == '5' or mouse.Target.Name == '6' then
				if not mouse.Target:FindFirstChild('selection') and mouse.Target.Parent == workspace.Valves and mouse.Target.Using.Value == false and script.Parent.IsUsingValve.Value == false then
					box = script.Parent.ValveUI.selection:clone()
				box.Parent = mouse.Target
				
					selecting = true
					box.Adornee = mouse.Target
				end
		else
				selecting = false
				if box then
					box:Destroy()
				end
			end
		
	end	
end

