Reclining Chair Script

Hi everyone!

I am attempting to make a recliner chair in which the back of the chair reclines and the seat reclines with it. I have got the section of script at the top to move the seat, and the section of script at the bottom to recline the back of the chair. I would like to link the 2 scripts together if possible so that when the click detector is pressed, the back of the chair reclines and the seat moves backwards.

Would anyone be able to rearrange the script slightly to help me to achieve this?

Script is pasted below.

local open = false
local part = script.Parent.Parent.Mainchair.Seat
local tweenService = game:GetService("TweenService")

local properties = {
	["Size"] = Vector3.new(1.31, 0.12, 1.17),
	["Color"] = Color3.fromRGB(1.31, 0.12, 1.17)
}

local tweenInfo = TweenInfo.new(
	1.5, -- seconds
	Enum.EasingStyle.Linear, -- style
	Enum.EasingDirection.Out, -- how moves
	0, -- repeat count
	false, -- reverse
	0 -- delay
)

local tween = tweenService:Create(part, tweenInfo, properties)

tween:Play()

script.Parent.Main.ClickDetector.MouseClick:Connect(function()
	if open == false then
		open = true
		for i = 0, 50 do
			script.Parent:SetPrimaryPartCFrame(script.Parent.pivot.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(-0.5),0,0))
			wait()
		end
	else
		open = false
		for i = 0, 50 do
			script.Parent:SetPrimaryPartCFrame(script.Parent.pivot.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(0.5),0,0))
			wait()
		end
	end
end)

Thank you in advance, I would much appreciate any help with this, I’m not very experienced yet, as you can probably tell. :slight_smile:

1 Like

For starters, never place a Server Script in Workspace or ReplicatedStorage; always keep those parented to ServerScriptStorage, unless you’re using a Local Script.

-- Services
local TweenService = game:GetService("TweenService")

-- Booleans
local ChairOpen = false

-- Definitions
local Chair = workspace:WaitForChild("Chair")
local BackSeat = Chair.Back
local FrontSeat = Chair.Front.Seat

local function createTween(targetCFrame)
	local tweenInfo = TweenInfo.new(
		1.5, -- seconds
		Enum.EasingStyle.Linear, -- style
		Enum.EasingDirection.Out, -- direction
		0, -- repeat count
		false, -- reverse
		0 -- delay
	)

	local tweenGoal = { CFrame = targetCFrame }
	return TweenService:Create(BackSeat, tweenInfo, tweenGoal)
end

local ReclineForwardAngle = 20 -- Adjust this to your liking

local reclineForward = BackSeat.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(-ReclineForwardAngle), 0, 0)
local reclineBackward = BackSeat.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(0), 0, 0)

local tweenForward = createTween(reclineForward)
local tweenBackward = createTween(reclineBackward)

Chair.Back.ClickDetector.MouseClick:Connect(function()
	if not ChairOpen then
		ChairOpen = true
		tweenForward:Play()
	else
		ChairOpen = false
		tweenBackward:Play()
	end
end)

If you want to apply this to all chairs:

-- Services
local TweenService = game:GetService("TweenService")

for _, Chair in ipairs(workspace:GetDescendants()) do
	if Chair:IsA("Model") and Chair.Name == "Chair" then
		local ChairOpen = false
		
		local BackSeat = Chair.Back
		local FrontSeat = Chair.Front.Seat

		local function createTween(targetCFrame)
			local tweenInfo = TweenInfo.new(
				1.5, -- seconds
				Enum.EasingStyle.Linear, -- style
				Enum.EasingDirection.Out, -- direction
				0, -- repeat count
				false, -- reverse
				0 -- delay
			)

			local tweenGoal = { CFrame = targetCFrame }
			return TweenService:Create(BackSeat, tweenInfo, tweenGoal)
		end

		local ReclineForwardAngle = 20 -- Adjust this to your liking

		local reclineForward = BackSeat.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(-ReclineForwardAngle), 0, 0)
		local reclineBackward = BackSeat.CFrame * CFrame.fromEulerAnglesXYZ(math.rad(0), 0, 0)

		local tweenForward = createTween(reclineForward)
		local tweenBackward = createTween(reclineBackward)

		Chair.Back.ClickDetector.MouseClick:Connect(function()
			if not ChairOpen then
				ChairOpen = true
				tweenForward:Play()
			else
				ChairOpen = false
				tweenBackward:Play()
			end
		end)

	end
end

If you want the front of the seat to recline along with the back, please let me know.

1 Like

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