Computer says 'nil'

Hello, I’ve been trying to make a lock-on script that allows you to still control the pitch/tilt/y-axis. Now I’m trying to frankenstein together a tween-based cframe setting of the position and orientation of the camera. BUT. I have this angle variable that’s the orientation gleaned from the mousemovement. but i keep getting the error that ‘angle’ is nil - even when i put the mousemovement code INTO the lockOncamer function. what am i doing wrong here?? (the other question i’ve got is wheter angle should be angle.lookvector but i cant even test it because it only registers as nil within the lockon function)
thank you very much for any help!!!

local TWEEN_SERVICE = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
startPos = nil
endPos = nil
angle = CFrame.new(0,0,0)

local function getLockOnPos(target, targetRoot)
	local startPos = CFrame.lookAt(Root.Position,targetRoot.Position) * CFrame.new(0,10, 13)
	-- this influences how far the camera is set from the avatar (the thing behind is the offset, last number is zoom)

-- get how much the mouse moves:
	local mouseDelta = uis:GetMouseDelta()
	if mouseDelta ~= Vector2.new(0, 0) then

		local pastAngleX, pastAngleY, pastAngleZ = Camera.CFrame:ToOrientation()
		angle = CFrame.fromOrientation(pastAngleX + math.rad(mouseDelta.Y), pastAngleY + math.rad(mouseDelta.X), math.rad(0))
end
	
	return startPos,angle
end


-- original way to set camera orientation:
 --Camera.CFrame = CFrame.new(workspace.CurrentCamera.CFrame.Position) * angle

	
local function lockOnCamera()
	Camera.CameraType = Enum.CameraType.Scriptable
	while renderWait() do
		if not conditions() then
			break
		end
		
		startPos, angle = getLockOnPos(enemytobefaced, enemytobefaced.HumanoidRootPart)
		print (angle)
		TWEEN_SERVICE:Create(Camera,TweenInfo.new(0.1,Enum.EasingStyle.Quad),{CFrame = CFrame.lookAt(startPos.Position, angle)}):Play()
	
	end
end

I’ma put some prints in this function and tell me what it says

local function getLockOnPos(target, targetRoot)
	local startPos = CFrame.lookAt(Root.Position,targetRoot.Position) * CFrame.new(0,10, 13)
	-- this influences how far the camera is set from the avatar (the thing behind is the offset, last number is zoom)

-- get how much the mouse moves:
	local mouseDelta = uis:GetMouseDelta()
print(mouseDelta)
	if mouseDelta ~= Vector2.new(0, 0) then

		local pastAngleX, pastAngleY, pastAngleZ = Camera.CFrame:ToOrientation()
		angle = CFrame.fromOrientation(pastAngleX + math.rad(mouseDelta.Y), pastAngleY + math.rad(mouseDelta.X), math.rad(0))
print(angle)
end
print(angle)	
	return startPos,angle
end
1 Like

As @StraightScared said, use some print statements.

        print(enemytobefaced, enemytobefaced.HumanoidRootPart)		
		startPos, angle = getLockOnPos(enemytobefaced, enemytobefaced.HumanoidRootPart)
        print(startPos)

to test it?
I’ve done a lot of printing to test. Why these specific print statements? Am i missing something here?

For my prints in the other function its to see if angle is being set and sent over

1 Like

Oh thanks. THeres actually some more code in the script that i didnt think would be relevant but maybe it is

local lockedOn = false

--[[Utility]]--
-- FOR THE WHILE LOOP CONTINUOUSLY CHECKING
-- this function is called to check for every renderstep, 
-- but onlly when the function is called
local function renderWait()
	game:GetService("RunService").RenderStepped:Wait()
	
	return true
end

--CONDITIONS
local function conditions()
	if not lockedOn then return end
	return true
end



EVENTS.LockOnToggle.Event:Connect(function(target)
	if target then
		lockedOn = true
		enemytobefaced = target
		lockOnCamera()
	else
		lockedOn = false
		Camera.CameraType = Enum.CameraType.Custom
		
	end
end)

Yeah, this was really informative, is the code in a server or local script.

A lot of times communication between the server and client can make data go nil, for example, sending a mouse. or maybe the target itself could be nil from sending it over.

1 Like

The reason for prints like this is to see what each variable is before or after a calculation.
If there are 10 steps in a calculation and the result is nil then this helps to find which step is giving an unexpected answer. It then helps to locate the calculations that get the variables for that step to track down the problem.

1 Like

Its in a localscript. I added uis.MouseBehavior = Enum.MouseBehavior.LockCenter to the getLockonPos function. i seem to now be able to print ‘angle’ from the lockoncamera function. So it is no longer nil! BUT. i dont know what to put in the second argument (which originally was Endpos.Position). Angle.Lookvector gives strange behaviour and doesnt update.

Dear StraightScared programmer,

Thank you for replyign and looking into things seriously. I have decided to give up (i’ve been struggling for days and days with this and i must conclude CFrames is just a no). I messed up the code and the camera didn’t automatically turn anyway and i thought…you know what, its fine like this.ha. I will try to rebuild this script to do a quick adjustment to camera on the x axis after a dash and directly after lockon.

Thanks again!
Regards
Knijn

I may be pushing it now but I hope you will take one more look at this hugely simplified script.

The problem: Im tryint to tween the camera to face the enemy. This works. But then whether i used a scriptable camera or simply moved the custom camera - after the tween plays, the camera ‘goes back’ to its original zoom distance. When i set the cframe outside of the tween, this does not happen.

Do you see any way to fix this?
thank you very much

local TWEEN_SERVICE = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
startPos = nil
endPos = nil


--[[Folders]]
local EVENTS = script.Parent.Parent:WaitForChild("Events")

--[[Camera]]--
local Camera = workspace.CurrentCamera

--[[Character]]--
local char = script.Parent.Parent
local Root = char:WaitForChild("HumanoidRootPart")

local RunService = game:GetService("RunService")

local enemytobefaced

local combatData = char:WaitForChild("combatData")

local PastCameraPosition = Camera.CFrame.Position

--[[Scripting]]--
local lockedOn = false

local function getLockOnPos(target, targetRoot)
	uis.MouseBehavior = Enum.MouseBehavior.LockCenter
	local startPos = CFrame.lookAt(Root.Position,targetRoot.Position) * CFrame.new(0,10, 13)
	-- this influences how far the camera is set from the avatar (the thing behind is the offset, last number is zoom)

	local endPos = targetRoot.CFrame * CFrame.new(0,-4,0)

	-- this y number influences the tilt/pitch, noramlly -4

	return startPos,endPos
end

local function lockOnCamera()
	--Camera.CameraType = Enum.CameraType.Scriptable

		startPos, endPos = getLockOnPos(enemytobefaced, enemytobefaced.HumanoidRootPart)

		--TWEEN_SERVICE:Create(Camera,TweenInfo.new(0.3,Enum.EasingStyle.Quad),{CFrame = CFrame.lookAt(startPos.Position,endPos.Position)}):Play()
	
	Camera.CFrame = CFrame.lookAt(startPos.Position,endPos.Position)
	--Camera.CameraType = Enum.CameraType.Custom
end

--Bindables

EVENTS.LockOnToggle.Event:Connect(function(target)
	if target then
		lockedOn = true
		enemytobefaced = target
		lockOnCamera()
	else
		lockedOn = false
		Camera.CameraType = Enum.CameraType.Custom

	end
end)