Changing Camera Cframe to a Spacific part.Cframe

I have a vehicle in my game and I want it to have a similar first-person mode as jailbreak. (In jailbreak when you press C , the Cframe of the Camera changes to the Cframe of an invisible part inside the car which represents the first-person camera.)

Now, I want the camera Cframe changed only for the player on the driver’s seat but not the entire server’s players and to achieve that I have prepared three scripts.
One, that enables the server event script whenever it detects a player has entered the driver seat.

local Seat = script.Parent

Seat.Changed:Connect(function()   
	
	if Seat.Occupant ~= nil  then
         script.Parent.camscript.Disabled=false

		
	else script.Parent.camscript.Disabled=true
	
		end
		end)
       

Second, a script that fires an event to the client whenever the player in the driver seat presses C

local userInput = game:GetService("UserInputService")


script.Parent.Touched:Connect(function(hit)
	

		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		userInput.InputBegan:Connect(function(key, processed)
		

			if key.KeyCode == Enum.KeyCode.C then
			game.ReplicatedStorage["CARCAMERA"]:FireClient(player)
			
			print("Pressed C")
			
		end	
		end)

end)

And the last script in the StarterPlayerScripts receives the fired event and changes the player’s camera,Cframe to that part named sakuracam1.Cframe.


local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")



game.ReplicatedStorage:WaitForChild("CARCAMERA").OnClientEvent:connect(function()

	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = part.CFrame
	print("camera to cframe")



end)

But It is not working(The player’s camera Cframe is not changing to the parts Cframe ) when I am running the game and pressing C while sitting in the driving seat.

Can someone please help me to overcome this issue?

2 Likes

does the script in StarterPlayerScripts need a loop?

I don’t think so, because isn’t it get activated every time I fire the server event?

did the code running properly no errors?

There are no errors but it is also not working because it is not feedbacking with the print text.

I think the second script is not working.

is it supposed to be CFrame not “Cframe”?

1 Like

You arent setting the CameraType to Scriptable.

1 Like

Ah right, I will make it scriptable and try again.

I have changed the client script to this. But not working and also In the second script I mentioned in the thread (server event script) I have added a print section where is it supposed to say pressed C whenever I successfully fire to the client but it is not popping up in the output tab.


local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")



game.Players.LocalPlayer:WaitForChild("CARCAMERA").OnClientEvent:connect(function()
	repeat Camera.CameraType = Enum.CameraType.Scriptable wait() until Camera.CameraType == Enum.CameraType.Scriptable
	
	Camera.Cframe = part.Cframe
	print("camera to cframe")
	
	
	
end)

Here is a little something I made that can change the camera

Edit: you don’t need a server script for the camera, just local

1 Like

Yes, I changed it to CFrame but no luck.

local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")



game.Players.LocalPlayer:WaitForChild("CARCAMERA").OnClientEvent:connect(function()
	
	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = part.CFrame
	print("camera to cframe")
	
	
	
end)

how about this

1 Like

Yes, this script is perfect, but I think the problem is In the second script I mentioned in the thread (server event script) I have added a print section where is it supposed to say pressed C whenever I successfully fire to the client but it is not popping up in the output tab. So I think it is not firing to the clint event.

where does the RemoteEvent placed tho?

But I wanted to make sure that the camera only moves to that specific vehicle part only when a player is driving.

Screenshot_58
Here

game.ReplicatedStorage:WaitForChild("CARCAMERA").OnClientEvent:connect(function()

second script?

1 Like

I have updated the clint side script to this

local Camera = workspace.CurrentCamera
local part = workspace:WaitForChild("sakuracam1")



game.ReplicatedStorage:WaitForChild("CARCAMERA").OnClientEvent:connect(function()

	Camera.CameraType = Enum.CameraType.Scriptable
	Camera.CFrame = part.CFrame
	print("camera to cframe")



end)

And this is the server event script that fires to the client

local userInput = game:GetService("UserInputService")


script.Parent.Touched:Connect(function(hit)
	

		local player = game.Players:GetPlayerFromCharacter(hit.Parent)

		userInput.InputBegan:Connect(function(key, processed)
			if processed then
				return
			end

			if key.KeyCode == Enum.KeyCode.C then
			game.ReplicatedStorage["CARCAMERA"]:FireClient(player)
			
			print("Pressed C")
			
		end	
		end)
	
		
			

	
end)
1 Like

does it work? does it have any errors?