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
Have you tried using Position?
Position won’t work because I am setting the CFrame and there is no Position property for the camera
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)
Works just fine, I am pretty sure it is something to do with the CameraSubject
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.
Getting an error of “Camera is not a valid member of Player”
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.
Change your camera type to script / scripted!
A scriptable camera type will not let the camera move as the player moves
That is what I previously had.
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.
It’s been proven that it fires and it does get set to in and the code runs:
Where is the local script located?
It’s located right inside of a tool
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.