Unable to get the local player's camera

The idea is that when a player sits in a seat their camera gets tweened and positioned using the basic camera manipulation:

local Seat = workspace.PC.Seat.Seat

local camera = workspace.CurrentCamera
local TweenService = game:GetService("TweenService")

local StartPos = Vector3.new(1.186, 4.963, -43.564)
local TargetPos = Vector3.new(1.201, 4.97, -46.22)

local tween = TweenService:Create(
	camera,
	TweenInfo.new(1.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out),
	{
		CFrame = CFrame.new(1.186, 4.963, -43.564),
		Focus = CFrame.new(1.201, 4.97, -46.22)
	}
)



Seat.ChildAdded:connect(function(Child)
	if Child.Name == "SeatWeld" then
		camera.CameraType = Enum.CameraType.Scriptable
		camera.FieldOfView = 45
		tween:Play()
		wait(1.5)
		camera.CFrame = CFrame.new(StartPos, TargetPos)
	end
end)
Seat.ChildRemoved:connect(function(child)
	if child.Name == "SeatWeld" then
		camera.FieldOfView = 70
		camera.CameraType = Enum.CameraType.Custom
	end
end)

This is placed inside a script(which is placed inside the seat) with RunContext set to Client, though when someone sits in it every player in-game will get their camera tweened too.

I’ve even tried with Events and more, but this is still bugging me. So if anyone has an idea for a fix I would be glad.

Put the code in a LocalScript, and change the Seat.ChildAdded and Seat.ChildRemoved to Remote events in Replicated Storage, then put Seat.ChildAdded and Seat.ChildRemoved events in a server script, and call the Remote events in the Replicated Storage.

Could you explain this a little more in-depth?

I could write some example code:

-- server script
local Seat = script.Parent
local rs = game:GetService("ReplicatedStorage")
local chairEvents = rs.ChairEvents

local sit = chairEvents.Sit
local out = chairEvents.Out

Seat.ChildAdded:connect(function(Child)
	if Child.Name == "SeatWeld" then
		local plr = -- get the player from the weld (I don't know how to do this on the top of my head)
		sit:FireClient(plr)
	end
end)

Seat.ChildRemoved:connect(function(Child)
	if Child.Name == "SeatWeld" then
		local plr = -- get the player from the weld (I don't know how to do this on the top of my head)
		out:FireClient(plr)
	end
end)

Now in the Client script just replace the Seat.ChildAdded with sit and Seat.ChildRemoved with out events.

1 Like

Should I just put the Client script in the StarterPlayerScripts?

Yes, that would be the ideal place.

Well, it apparently really was my bad script placement, now it works perfectly, thank you!

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