Setting Camera's CFrame not working

I am trying to make the player’s camera teleport towards the yellow part you can see in the video below, but it is not working and is instead just not moving the camera’s CFrame at all, is there any way to fix this?
(didn’t get any errors)

Here’s the code

script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	if inorout == "In" then
		game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
	else
		game.Workspace.CurrentCamera.CameraSubject = hum
	end
end)
1 Like

Is it a localscript? because you can’t change a player’s camera with a server script

Yeah its a localscript, everything prints so i know the event fires and it gets to the point where it changes the camera

1 Like

Have you tried using Position?

Position won’t work because I am setting the CFrame and there is no Position property for the camera

1 Like

Ok, then try something like this to find out what is causing it?

script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	print(inorout)
	if inorout == "In" then
	print("Viewing GunAim Part")
		game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
	else
	print("Normal Camera")
		game.Workspace.CurrentCamera.CameraSubject = hum
	end
end)

image
Works just fine, I am pretty sure it is something to do with the CameraSubject

1 Like

Set CameraSubject to the part or a humanoid placed in the part??

WAIT!!! Have you set camera type to scripted / script ?

It looks like the issue might be that you’re using game.Workspace.CurrentCamera instead of player.Camera . In Roblox, every player has their own camera object, so you need to use player.Camera to reference the camera associated with the local player.

Try changing this line:

game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame

to this:

player.Camera.CFrame = script.Parent.GunAim.CFrame

This should set the camera’s CFrame to the position and orientation of the GunAim part.

1 Like

Getting an error of “Camera is not a valid member of Player”

1 Like

It looks like the Camera property of the Player object has been removed in a recent update of Roblox. Instead, you can use the Workspace.CurrentCamera object, which is the camera that is currently being rendered to the player’s screen.

try changing this line:

player.Camera.CFrame = script.Parent.GunAim.CFrame

to this:

game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame

This should set the camera’s CFrame to the position and orientation of the GunAim part.

1 Like

Change your camera type to script / scripted!

A scriptable camera type will not let the camera move as the player moves

1 Like

That is what I previously had.

1 Like

In that case, it might be that the SetCamera event is not being fired, or that the argument passed to the event handler is not equal to "In" . You can add a print statement to the event handler to see what the value of inorout is when the event is fired:

script.Parent.SetCamera.OnClientEvent:Connect(function(inorout)
	print(inorout)
	local player = game.Players.LocalPlayer
	local char = player.Character or player.CharacterAdded:Wait()
	local hum = char:WaitForChild("Humanoid")
	if inorout == "In" then
		game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
	else
		game.Workspace.CurrentCamera.CameraSubject = hum
	end
end)

This will print the value of inorout to the output window, which can help you determine if the event is being fired and what the argument is.

1 Like

It’s been proven that it fires and it does get set to in and the code runs:

Where is the local script located?

1 Like

It’s located right inside of a tool

1 Like

It’s parent is the same as the part’s parent, as you can see from the code.