Setting Camera's CFrame not working

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.

In that case, it’s possible that the GunAim part doesn’t exist in the script.Parent object. You can add a check to see if the part exists before trying to access its CFrame property:

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
		if script.Parent:FindFirstChild("GunAim") then
			game.Workspace.CurrentCamera.CFrame = script.Parent.GunAim.CFrame
		else
			print("GunAim part not found")
		end
	else
		game.Workspace.CurrentCamera.CameraSubject = hum
	end
end)

This will print an error message to the output window if the GunAim part is not found in the script.Parent object, which can help troubleshoot the issue further.

1 Like