How do I fix index with nil error? Roblox Studio

Hello, I am currently working on an fps game rn. I have added some animations to it, and when I added the unequip animation in the loadslot function, I got an error on line 142, " 19:34:34.396 Workspace.IsaiahCoolKidYo.FrameworkClient:142: attempt to index nil with ‘FakeCamera’ - Client - FrameworkClient:142". Does anyone know how to fix this?

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local humanoid = character:WaitForChild("Humanoid")

local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

local camera = workspace.CurrentCamera

local aimCFrame = CFrame.new()

local mouse = player:GetMouse()

local isAiming = false
local isShooting = false
local isReloading = false
local isSprinting = false
local canShoot = true

local debounce = false

local currentSwayAmount = -.3
local swayAmount = -.3
local aimSwayAmount = .2
local swayCFrame = CFrame.new()
local lastCameraCFrame = CFrame.new()
local bobOffset = CFrame.new()

local fireAnimation = nil
local equipAnimation = nil
local uneqipAnimation = nil

local framework = {
	inventory = {
		"GLOCK17";
		"M4A1";
		"Knife";
		"Frag";
	};
	

	module = nil;
	viewModel = nil;
	currentSlot = 1;
}

function loadSlot(item)
	if isReloading == false then
		local viewModelFolder = game.ReplicatedStorage.Viewmodels
		local moduleFolder = game.ReplicatedStorage.Modules

		for i, v in pairs(camera:GetChildren()) do
			if v:IsA("Model") then
				unequipAnimation:Play()
				unequipAnimation.Stopped:Wait()
				v:Destroy()
			end
		end

		if moduleFolder:FindFirstChild(item) then
			framework.module = require(moduleFolder:FindFirstChild(item))

			if viewModelFolder:FindFirstChild(item) then
				framework.viewModel = viewModelFolder:FindFirstChild(item):Clone()
				framework.viewModel.Parent = camera

				if framework.viewModel and framework.module and character then
					fireAnimation = Instance.new("Animation")
					fireAnimation.Parent = framework.viewModel
					fireAnimation.Name = "FireAnimation"
					fireAnimation.AnimationId = framework.module.fireAnimation
					fireAnimation = framework.viewModel.AnimationController.Animator:LoadAnimation(fireAnimation)
					
					equipAnimation = Instance.new("Animation")
					equipAnimation.Parent = framework.viewModel
					equipAnimation.Name = "EquipAnimation"
					equipAnimation.AnimationId = framework.module.equipAnimation
					equipAnimation = framework.viewModel.AnimationController.Animator:LoadAnimation(equipAnimation)
					
					uneqipAnimation = Instance.new("Animation")
					uneqipAnimation.Parent = framework.viewModel
					uneqipAnimation.Name = "UnequipAnimation"
					uneqipAnimation.AnimationId = framework.module.unequipAnimation
					uneqipAnimation = framework.viewModel.AnimationController.Animator:LoadAnimation(uneqipAnimation)

					game.ReplicatedStorage.Events.LoadSlot:FireServer(framework.module.fireSound.SoundId, framework.module.fireSound.Volume)
					
					if framework.viewModel then
						for i, v in pairs(framework.viewModel:GetDescendants()) do
							if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
								v.Transparency = 1
							end
						end
					end
					
					equipAnimation:Play()
					
					task.wait(.1)
					
					if framework.viewModel then
						for i, v in pairs(framework.viewModel:GetDescendants()) do
							if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("BasePart") then
								if v.Name == "Main" or v.Name == "Muzzle" or v.Name == "FakeCamera" or v.Name == "AimPart" then
									
								else
									v.Transparency = 0
								end
							end
						end
					end
				end
			end
		end
	end
end

function shoot()
	if framework.module.fireMode == "Semi" then
		equipAnimation:Stop()
		fireAnimation:Play()
		framework.module.ammo -= 1

		game.ReplicatedStorage.Events.Fire:FireServer(framework.viewModel.Muzzle.Position, mouse.Hit.Position, framework.module.damage, framework.module.headshotDamage)

		debounce = true

		wait(framework.module.debounce)

		debounce = false
	end

	if framework.module.fireMode == "Full Auto" then
		isShooting = true
	end
end

local oldCameraCFrame = CFrame.new()

function updateCameraShake()
	local newCameraCFrame = framework.viewModel.FakeCamera.CFrame:ToObjectSpace(framework.viewModel.PrimaryPart.CFrame)
	camera.CFrame = camera.CFrame * newCameraCFrame:ToObjectSpace(oldCameraCFrame)
	oldCameraCFrame = newCameraCFrame
end

local hud = player.PlayerGui:WaitForChild("Hud")

runService.RenderStepped:Connect(function()
	mouse.TargetFilter = framework.viewModel
	
	local rotation = camera.CFrame:ToObjectSpace(lastCameraCFrame)
	local x,y = rotation:ToOrientation()
	swayCFrame = swayCFrame:Lerp(CFrame.Angles(math.sin(x) * currentSwayAmount, math.sin(y) * currentSwayAmount, 0), .1)
	lastCameraCFrame = camera.CFrame
	
	if framework.viewModel and framework.module then
		hud.GunName.Text = framework.inventory[framework.currentSlot]
		hud.Ammo.Text = framework.module.ammo
		hud.Ammo.MaxAmmo.Text = framework.module.maxAmmo
	end
	
	if humanoid then
		if humanoid.MoveDirection.Magnitude > 0 then
			if humanoid.WalkSpeed == 10 then
				bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 4) * .05, -humanoid.CameraOffset.y/3, 0) * CFrame.Angles(0, math.sin(tick() * -4) * -.05, math.cos(tick() * -4) * .05), .1)
				isSprinting = false
				
			elseif humanoid.WalkSpeed == 20 then
				bobOffset = bobOffset:Lerp(CFrame.new(math.cos(tick() * 8) * .1, -humanoid.CameraOffset.y/3, 0) * CFrame.Angles(0, math.sin(tick() * -8) * -.1, math.cos(tick() * -8) * .1) * framework.module.sprintCFrame, .1)
				isSprinting = true
			end
			
		else
			bobOffset = bobOffset:Lerp(CFrame.new(0, -humanoid.CameraOffset.y/3, 0), .1)
			isSprinting = false
		end

		for i, v in pairs(camera:GetChildren()) do
			if v:IsA("Model") then
				v:PivotTo(camera.CFrame * swayCFrame * aimCFrame * bobOffset)
				updateCameraShake()
			end
		end
	end
	
	if isAiming and framework.viewModel ~= nil and framework.module.canAim and isSprinting == false then
		local offset = framework.viewModel.AimPart.CFrame:ToObjectSpace(framework.viewModel.PrimaryPart.CFrame)
		aimCFrame = aimCFrame:Lerp(offset, framework.module.aimSmooth)
		currentSwayAmount = aimSwayAmount
		
	else
		local offset = CFrame.new()
		aimCFrame = aimCFrame:Lerp(offset, framework.module.aimSmooth)
		currentSwayAmount = swayAmount
	end
end)

loadSlot(framework.inventory[1])

userInputService.MouseIconEnabled = false

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		if framework.currentSlot ~= 1 and isReloading == false then
			loadSlot(framework.inventory[1])
			framework.currentSlot = 1
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Two and isReloading == false then
		if framework.currentSlot ~= 2 then
			loadSlot(framework.inventory[2])
			framework.currentSlot = 2
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Three and isReloading == false then
		if framework.currentSlot ~= 3 then
			loadSlot(framework.inventory[3])
			framework.currentSlot = 3
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Four and isReloading == false then
		if framework.currentSlot ~= 4 then
			loadSlot(framework.inventory[4])
			framework.currentSlot = 4
		end
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = true
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if character and framework.viewModel and framework.module and framework.module.ammo > 0 and debounce == false and isReloading ~= true and canShoot == true then
			shoot()
		end
	end
	
	if input.KeyCode == Enum.KeyCode.R then
		canShoot = false
		isReloading = true
		
		wait(framework.module.reloadSpeed)
		
		canShoot = true
		isReloading = false
		framework.module.ammo = framework.module.maxAmmo
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isAiming = false
	end
end)

humanoid.Died:Connect(function()
	if framework.viewModel then
		framework.viewModel:Destroy()
	end
end)

framework.module.ammo = framework.module.maxAmmo

while wait() do
	if isShooting and framework.module.ammo > 0 and isReloading ~= true and canShoot == true then
		fireAnimation:Play()
		framework.module.ammo -= 1
		
		game.ReplicatedStorage.Events.Fire:FireServer(framework.viewModel.Muzzle.Position, mouse.Hit.Position, framework.module.damage, framework.module.headshotDamage)
		
		mouse.Button1Up:Connect(function()
			isShooting = false
		end)
		
		wait(framework.module.fireRate)
	end
end
local framework = {
	viewModel = nil;
}

This happens because framework.viewModel returns nil, and you tried to find an item inside nothing, which will error.

framework.viewModel = viewModelFolder:FindFirstChild(item):Clone()

This line of code is the only thing that defines the viewModel key in the framework table.

Is the updateCameraShake function running before the loadSlot? Or the loadSlot function did not set the viewModel item? Either way, I recommend using an if statement to only run if framework.ViewModel actually exists.

function updateCameraShake()
	if framework.ViewModel then
		...
	end
end

I tried doing if “framework.viewModel then” before, but it doesn’t work for some reason.

Since the updateCameraShake function runs continuously, and you added an if statement to check, this means that the framework.viewModel item was never defined.

framework.viewModel = viewModelFolder:FindFirstChild(item):Clone()

This line of code is guarded by 3 if statements, so maybe check if they do run by setting multiple print statements.

Also, the viewModel item is defined by cloning an object. Is it possible that this object has its Archivable property set to true?

I got it to sometimes work by putting the inside of updateCameraShake in “if framework.viewmodel” and the part that says for i, v in pairs in “if framework.viewmodel”, which is in the loasSlot function. It sometimes error saying “FakeCamers is not a valid member of model glock17.” It still is doing something to run the error.