Player camera issue

I have made a post similarly to this earlier in the day about the player’s camera tweening to a specific location upon sitting in a seat. While my previous issue has been solved, I have encountered a new one when I was making modifications. As stated: I had made a system where the player’s camera would tween to a specific location upon sitting on a specific seat. Upon realizing that I wanted to have more seats in the game, I hit a roadblock when I realized that I would have to manually acquired each seat and the camera it is attached to, which isn’t efficient at all. So as an alternative to a single LocalScript inside of the StarterGui to control all of the cameras at once, I assumed that it would be better to control the camera from within the seat itself. The code works as expected for only one player. When other players join and sit in the other seats, however, it causes the player with the working camera to revert back to default. So the overall issue is that only one player at a time that is seated will have the working camera positioning. For the script I currently have, it is a regular script with a client run context, and it is inside of every seat present in my game. My apologies if this is a long read. Hopeful to hear any response.

-- Get services
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

-- Get the player
local player = Players.LocalPlayer

-- Get instances
local seat = script.Parent
local monitorCamera = seat:WaitForChild("MonitorCamera")
local currentCamera = workspace.CurrentCamera

-- Set tween
local cameraTween = TweenService:Create(currentCamera, TweenInfo.new(1.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.InOut), {CFrame = monitorCamera.CFrame})

-- Function to change the player's camera upon sitting down
local function updateCamera()
	if seat.Occupant and seat.Occupant.Parent == player.Character then
		currentCamera.CameraType = Enum.CameraType.Scriptable
		cameraTween:Play()
	else
		currentCamera.CameraType = Enum.CameraType.Custom
	end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(updateCamera)

-- Check if the player is sitting on game start
updateCamera()
1 Like

(Just a heads up your earlier method of one script is much more malleable in the future than what you’re currently doing!)

The problem is simple to identify, your conditional statement does not care what is happening to you if someone else sits down. It’s the else bit that’s causing you trouble. If someone sits down in another seat, that local code is still going to fire for you and run into that else because the first condition will never be met (since… the occupant is not you).

Fortunately this is a simple fix. Just cache which character sat down in a variable outside of UpdateCamera(). Then you can just change that else to:
elseif lastSeatedOccupant == player.Character then

You can also just throw this line in at the bottom of UpdateCamera() to update the cache:
lastSeatedOccupant = seat.Occupant and seat.Occupant.Parent

1 Like

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