Attempt to index nil with 'C0'

how to remve bug

local players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local tool = script.Parent

local cam = workspace.CurrentCamera

local plr = players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")

local neck = char:FindFirstChild("RightHand"):FindFirstChild("RightWrist")

local y = neck.C0.Y

local active = false

tool.Equipped:Connect(function()
	
	active = true
	
	while true do	
		
		task.wait()
		
		if active == true then
			if neck then
				local camDirection = hrp.CFrame:ToObjectSpace(cam.CFrame).LookVector
				
				TweenService:Create(neck, TweenInfo.new(0.5), {
					C0 = CFrame.new(0, y, 0) * CFrame.Angles(0, camDirection.X, 0) * CFrame.Angles(camDirection.Y, 0, 0),
				}):Play()
			end

			tool.Unequipped:Connect(function()
				active = false
			end)
		else
			neck.C0 = CFrame.new(0, y, 0) * CFrame.Angles(0, 0, 0)
		end
	end
end)

Try changing :FindFirstChild to :WaitForChild, and see if that solves your issue, also check if character isn’t nil

EDIT: I created a Tool with a part as a handle, and apparently it works fine with no errors, and I didn’t even change any code – just copied yours. Which line does this error come from?

1 Like

Please send the line the C0 error is on

There is a C0 error on line 15

line 15 local y = neck.C0.Y

There is a C0 error on line 15

line 15 local y = neck.C0.Y

Check if char isn’t nil or if neck isn’t nil using print statements, also there are no errors apart from this?

1 Like
local players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local tool = script.Parent

local cam = workspace.CurrentCamera

local plr = players.LocalPlayer

local active = false

tool.Equipped:Connect(function()
	
	active = true
	
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	if char then
		local hrp = char:FindFirstChild("HumanoidRootPart")
		local neck = char:FindFirstChild("RightHand"):FindFirstChild("RightWrist")

		local y = neck.C0.Y
		
		while true do	

			task.wait()

			if active == true then
				if neck and hrp then
					local camDirection = hrp.CFrame:ToObjectSpace(cam.CFrame).LookVector

					TweenService:Create(neck, TweenInfo.new(0.5), {
						C0 = CFrame.new(0, y, 0) * CFrame.Angles(0, camDirection.X, 0) * CFrame.Angles(camDirection.Y, 0, 0),
					}):Play()
				end

				tool.Unequipped:Connect(function()
					active = false
				end)
			else
				TweenService:Create(neck, TweenInfo.new(0.5), {
					C0 = CFrame.new(0, y, 0) * CFrame.Angles(0, 0, 0),
				}):Play()
			end
		end
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.