"attempt to index nil with 'aimSmooth'" and "attempt to index nil with 'canAim'"

I’ve been following this tutorial on how to make an fps and I’m on part 2 and I’ve followed everything this tutorial said and it still won’t work and I’ve checked everything and since this error is so specific I can’t get any help on this except for the fact that this means the script doesn’t know what ‘aimSmooth’ means.

Video

Framework script:

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

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local camera = game.Workspace.CurrentCamera

local aimCF = CFrame.new()

local isAiming = false

local framework = {
	inventory = {
		"M4A1";
		"M9";
		"Deagle";
		"Frag";
	};
	
	module = nil;
	viewmodel = nil;
	currentSlot = 1;
}

function loadSlot(item)
	local viewmodelFolder = game.ReplicatedStorage.ViewModels
	local moduleFolder = game.ReplicatedStorage.Modules
	
	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
	
	if moduleFolder:FindFirstChild(item) then
		framework.viewmodel = viewmodelFolder:FindFirstChild(item):Clone()
		framework.viewmodel.Parent = camera
	end
end

RunService.RenderStepped:Connect(function()
	for i, v in pairs(camera:GetChildren()) do
		if v:IsA("Model") then
			v:SetPrimaryPartCFrame(camera.CFrame * aimCF)
		end
	end
	
	if isAiming and framework.viewmodel ~= nil and framework.module.canAim then
		local offset = framework.viewmodel.AimPart.CFrame:ToObjectSpace(framework.viewmodel.PrimaryPart.CFrame)
		aimCF = aimCF:Lerp(offset, framework.module.aimSmooth)
		
	else
		local offset = CFrame.new()
		aimCF = aimCF:Lerp(offset, framework.module.aimSmooth)
	end
end)

loadSlot(framework.inventory[1])

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.One then
		if framework.currentSlot ~= 1 then
			loadSlot(framework.inventory[1])
			framework.currentSlot = 1
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Two then
		if framework.currentSlot ~= 2 then
			loadSlot(framework.inventory[2])
			framework.currentSlot = 2
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Three then
		if framework.currentSlot ~= 3 then
			loadSlot(framework.inventory[3])
			framework.currentSlot = 3
		end
	end
	
	if input.KeyCode == Enum.KeyCode.Four 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
end)

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

M9 module script:

local Settings = {
	
	canAim = true;
	aimSmooth = .15;
	
}

return Settings

M4A1 module script:

local Settings = {
	
	canAim = true;
	aimSmooth = .125;
	
}

return Settings

Nobody in the video comments seem to have a problem with this.

This right here you’re mentioning the module which is from a framework. But earlier in the code, the module is referenced as nil. I don’t see anything making the module variable into the actual settings module so maybe just do that?

1 Like

I’m afraid I’ve already tried that, also that is how it is referenced in the video which is also why I’m confused.
Oh yeah, the video timestamp is 6:20

Attempt to index nil means that the script cannot find the object. This often is because the path to the object is wrong.

For instance,
workspace.Checkpoints.Part would get the part from a checkpoints folder. However, if the checkpoints folder is not there, then it would return attempt to index nil.

In your code, you never initialized aimSmooth or canAim variables. Or at least, I can’t find it.

In your code, when you do framework.module.canAim, you are missing .Settings your canAim is in the settings table.

Try and do framework.module.Settings.canAim instead, or do framework.module.Settings[canAim]. Unfortunately, I don’t work with module scripts often. You could also do a for loop like:

for index, value in framework.module.Settings do 
     if index == 'canAim' then
        return value --returns the value of canAim.

This is the best I can do. Hope this helps! This is a wild guess btw, idk if its right lol

After reading your post further, I realized it is getting the module value from the table framework??? This doesn’t make any sense.

The script isn’t getting the module at all, only the values from the table, which is why its returning attempt to index nil

I know, but if you look at the video it works
time stamp is 6:20

Ah, i think i figured it out.

You never added this part of the code in:

(4:28 in the video)

This gets all the properties from the module. Hopefully, this fixes your problem!

2 Likes

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