Client-side camera function not working properly

Hello! I’m currently designing a system in which the player’s camera will tween in front of a computer screen upon sitting in a specific chair. While the code itself runs perfectly fine- it runs across all clients, which I don’t want. The script below is a client-side script located in the StarterGui. I’ve searched around for solutions to no avail.

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

-- Get instances
local seat = workspace.PISA:WaitForChild("Seat")
local monitorCamera = seat.Parent: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 then
		currentCamera.CameraType = Enum.CameraType.Scriptable
		cameraTween:Play()
	else
		currentCamera.CameraType = Enum.CameraType.Custom
	end
end

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

You aren’t verifying if the seat occupant is/isin’t the local player.

I tried this. Now the code doesn’t even run.

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

-- Get instances
local seat = workspace.PISA:WaitForChild("Seat")
local monitorCamera = seat.Parent:WaitForChild("MonitorCamera")
local currentCamera = workspace.CurrentCamera
local player = Players.LocalPlayer

-- 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 player == seat.Occupant then
		currentCamera.CameraType = Enum.CameraType.Scriptable
		cameraTween:Play()
	elseif seat.Occupant == nil then
		currentCamera.CameraType = Enum.CameraType.Custom
	end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(updateCamera)
-- Get services
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

-- Get local player
local localPlayer = Players.LocalPlayer

-- Get instances
local seat = workspace.PISA:WaitForChild("Seat")
local monitorCamera = seat.Parent: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 == localPlayer.Character then
        currentCamera.CameraType = Enum.CameraType.Scriptable
        cameraTween:Play()
    else
        currentCamera.CameraType = Enum.CameraType.Custom
    end
end

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

-- Initial check in case player is already seated
updateCamera()

the occupant of a seat is a humanoid, not a player, so you would need to get the player from the character

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