So I have some code:
camera.CFrame = CFrame.Angles(-outcome.X, -outcome.Y,0) + cameraPart.Position
this should do something but I get an error:
attempt to index nil with ‘Position’
But the part or cameraPart does in fact exist. So why is this happening?
I tried making the script wait until it exists but it just doesnt. Even when I load in it doesnt. Why???
Full code:
-- cameras
local camera = workspace.CurrentCamera -- the camera
local cameraPart = workspace:FindFirstChild('CameraPart') -- the cameraPart or where the camera will be
-- player
local player = game:GetService('Players').LocalPlayer -- player
local mouse = player:GetMouse() -- mouse
-- services
local run = game:GetService('RunService') -- runservice
-- values
local maxDegree = 30 -- how far you can turn at most
-- code
repeat -- repeat until camera starts following cameraPart
camera.CameraType = Enum.CameraType.Scriptable
task.wait()
until camera.CameraType == Enum.CameraType.Scriptable and workspace:FindFirstChild('CameraPart')
warn(cameraPart)
local connect = run.RenderStepped:Connect(function()
local np = Vector2.new(mouse.X,mouse.Y)
local center = Vector2.new(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
local difference = np - center
local outcome = Vector2.new(difference.Y/center.Y, (difference.X/center.X)*maxDegree)
camera.CFrame = CFrame.Angles(-outcome.X, -outcome.Y,0) + cameraPart.Position
end)