When a person sits in a seat, their camera locks onto a brick

So I’m making a game and I want to add a seat that when sat in, makes the players camera lock onto the computer Infront of them. I cannot figure out a way to script this since I have never used camera or seat functions.
By the way I am very new to scripting.

2 Likes

Hopefully you know the basics but I’ll try to explain it more simply. We’re going to be using :GetPropertyChangedSignal to detect when the Occupant property of the seat changes like so:

local Seat = your.path.to.the.seat
-- the path above is for you to define, 
-- you’ll see this a lot in devforum 
-- posts as a placeholder since 
-- they can’t actually know 
-- where you place the instance

local ComputerScreen = path.to.computerscreen

Seat:GetPropertyChangedSignal(“Occupant”):Connect(function()
     local Character = Seat.Occupant.Parent
     Character.Head.CFrame = CFrame.lookAt(Character.Head.Position, ComputerScreen.Position)
end)

I’m unsure if this will work since I’ve typed this out on mobile so lemme know if it works or not

2 Likes

This is what it looks like when in studio, there is an underline on (“Occupant”).
Do I put this in a local or normal script and where do I place the script like in ServerScriptStorage or the seat part?

You can place it in ServerScriptService itself since it doesn’t use the script keyword and it’s directly accessing the workspace keyword. But you can place it in the seat as well.

Could you tell me what the error is? You can test the game and get it from the output or you can hover your mouse over the red line

1 Like

this is what it says when i hover over the earror in the script
Screenshot 2023-11-18 194226
nothing happens when I sit in the seat

There are different types of quotation marks in Unicode, and the error is that this type of quotation mark is not used as a string delimiter. You have the U+201C and U+201D characters, but they don’t serve the same purpose as U+0022 (") does. Sidenote: I just noticed it gets auto-corrected if you type two quotation marks (U+2022)

@FluffyFeather05 Replace the current quotation marks with " and it will look like this one:

local Seat = workspace.ComputerSeat
-- the path above is for you to define, 
-- you’ll see this a lot in devforum 
-- posts as a placeholder since 
-- they can’t actually know 
-- where you place the instance

local ComputerScreen = workspace.ViewScreen

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
     local Character = Seat.Occupant.Parent
     Character.Head.CFrame = CFrame.lookAt(Character.Head.Position, ComputerScreen.Position)
end)
2 Likes

Every time i jump out of the chair it says this in console,
Screenshot 2023-11-18 204831
other than that nothing happens

Yeah you’re right my bad. I’m on mobile so I can’t type the “normal” quotation marks.

Sorry I forgot to add an if statement. Add something like this under the “Seat:GetPropertyChangedSignal” line (and wrap the code in the if statement:

if Seat.Occupant and Seat.Occupant.Parent:IsA("Model") then
2 Likes

The errors stopped but it still dose nothing, this is how the script looks:

	local Seat = workspace.ComputerSeat
-- the path above is for you to define, 
-- you’ll see this a lot in devforum 
-- posts as a placeholder since 
-- they can’t actually know 
-- where you place the instance

local ComputerScreen = workspace.ViewScreen

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant and Seat.Occupant.Parent:IsA("Model") then
	local Character = Seat.Occupant.Parent
		Character.Head.CFrame = CFrame.lookAt(Character.Head.Position, ComputerScreen.Position)
		end
end)		 	

You can modify the CurrentCamera to achieve this.

Put a LocalScript in StarterPlayerScripts and paste this inside:

local Seat = workspace.ComputerSeat
local ComputerScreen = workspace.ViewScreen
local CurrentCamera = workspace.CurrentCamera

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant then
		local newPosition = ComputerScreen.Position + ComputerScreen.CFrame.LookVector * 5
		
		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		CurrentCamera.CFrame = CFrame.lookAt(newPosition, ComputerScreen.Position)
	else
		CurrentCamera.CameraType = Enum.CameraType.Custom
	end
end)
1 Like

It says this in the console and doesn’t do anything when seat is sat in

I forgot to add a WaitForChild.

Code:

local Seat = workspace:WaitForChild("ComputerSeat")
local ComputerScreen = workspace:WaitForChild("ViewScreen")
local CurrentCamera = workspace.CurrentCamera

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant then
		local newPosition = ComputerScreen.Position + ComputerScreen.CFrame.LookVector * 5

		CurrentCamera.CameraType = Enum.CameraType.Scriptable
		CurrentCamera.CFrame = CFrame.lookAt(newPosition, ComputerScreen.Position)
	else
		CurrentCamera.CameraType = Enum.CameraType.Custom
	end
end)
2 Likes

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