I am trying to create a first-person spectating system in my game where you can spectate in the first person perspectives of other players. Whenever a player spawns, I insert this LocalScript into him/her:
wait() local camera = game.Workspace.CurrentCamera:Clone() camera.Name = "FirstPersonCamera" camera.Parent = script.Parent:FindFirstChild("Head") camera.CameraType = Enum.CameraType.Scriptable while wait(0.01) do camera.Focus = CFrame.new(script.Parent:FindFirstChild("Head").Position) end
What this basically does is insert a replica of CurrentCamera
from Workspace and stores it in the player's head. If I print the camera's Focus
value, it will properly print a CoordinateFrame
value in the output. Then, in a separate LocalScript in StarterPack, I have a "Spectate" script. This is my algorithm to get the spectator's camera to be in the view of the person he/she is spectating.
[VIEW SOURCE TO PROPERLY SEE CODE]
--[[ NOTE: This is not the only code in the script. It's only the significant part. PRE-CONDITION: "humanoid" is the humanoid of the player you are spectating. cam.CameraType = Enum.CameraType.Scriptable --]] local cam = game.Workspace.CurrentCamera while wait(0.01) do cam.Focus = game.Players:GetPlayerFromCharacter(humanoid.Parent).Character.Head:FindFirstChild("FirstPersonCamera").Focus end
This does not work. I'm getting an error saying attempting to index a nil value
on the line where I try to set cam.Focus
. Instead of using Focus
, I tried replacing it with cam.CoordinateFrame
. I still got the same error message.
I am sort of new to camera manipulation, so any help would be appreciated.