"Attempt to index nil with Idle"

Basically making my own FPS framework, and then suddenly this error occurs:

- Attempt to index nil with 'Idle'

When the parent of Idle isn’t even nil. Here’s the module:

local root = script.Parent;

local data = {
	animations = {
		
		viewmodel = {
			idle = root.animations.view.idle;
			shoot = root.animations.view.shoot;
			reload = root.animations.view.reload;
			equip = root.animations.view.equip;
		};
		
		
		player = {
			idle = root.animations.out.idle;
			hipfire = root.animations.out.hipfire;
			aimfire = root.animations.out.aimfire;
			reload = root.animations.out.reload;
		};
		
	};
	
	firing = {
		rpm = 857;
		magCapacity = 30;
	}
	
}

return data;

And here’s how I access it:

-- inserting into table
remotes:WaitForChild("new").OnServerInvoke = function(player)
	if not player.Character then return; end

	players[player.UserId] = {};
	local weaponTable = players[player.UserId];
	
	weaponTable.magData = {};
	weaponTable.weapons = {};
	
	for index, weaponName in pairs(defaultWeapons) do
		local weapon = weapons:FindFirstChild(weaponName):Clone();
		local weaponSettings = require(weapon.settings);
		
		print(weapon)
		print(weaponSettings.animations.player.idle);
		print(weaponSettings.animations.viewmodel.idle);
		
		weaponTable.weapons[weaponName] = { weapon = weapon; settings = weaponSettings; };
		
		console.log(weaponTable.weapons[weaponName].settings.animations.player.idle);
		
		weaponTable.magData[index] = { current = weaponSettings.firing.magCapacity; 
		spare = weaponSettings.firing.magCapacity * magazineCount};
		
		weapon.Parent = player.Character;
		weapon.Handle.backweld.Part0 = player.Character.Torso;
	end
	
	return defaultWeapons, weaponTable.magData;
end
-- getting it
remotes:WaitForChild("equip").OnServerInvoke = function(player, wepName)
	
	if players[player.UserId].currentWeapon then return; end
--	if not players[player.UserId].weapons[wepName] then return; end
	if not player.Character then return; end
	local weaponTable = players[player.UserId];

	weaponTable.currentWeapon = weaponTable.weapons[wepName];
	player.gun.Value = weaponTable.currentWeapon.weapon;
	
	weaponTable.currentWeapon.Parent = player.Character;
	weaponTable.currentWeapon.weapon.Handle.backweld.Part0 = nil;
	
	weaponTable.currentWeapon.weapon.Handle.weaponHold.Part0 = player.Character["Right Arm"];
	weaponTable.loadedAnimations.idle = player.Character.Humanoid:LoadAnimation(weaponTable.currentWeapon.settings.animations.player.idle);
	--weaponTable.currentWeapon.weapon.settings.animations.player.idle
	weaponTable.loadedAnimations.idle:Play(0.4);
	
	return true;
end

why is this happening?

try to use FindFirstChild on finding the idle

1 Like

Apparently this is not the issue, I’ve already printed Idle whilst inserting into weaponTable and it printed it out fine.

Where does the error occur in your code? I don’t see an “Idle” with a capitalized I anywhere in your code.

Does the error specify the script or line?

1 Like

@goldenstein64 not Idle, idle.

@Voliiqs yes, in this line.

1 Like

Before the error line, can you print the contents of weaponTable.currentWeapon.settings.animations and see if you can recognize it as some other data structure?

e.g:

-- getting it code...
weaponTable.currentWeapon.weapon.Handle.weaponHold.Part0 = player.Character["Right Arm"];

table.foreach(weaponTable.currentWeapon.settings.animations, print)
--[[ expected output

viewmodel table: XXXXXXXX
player table: XXXXXXXX

--]]

weaponTable.loadedAnimations.idle = player.Character.Humanoid:LoadAnimation(weaponTable.currentWeapon.settings.animations.player.idle);

It printed the entire thing fine,
image

1 Like

That is really strange - the error message makes me think that ...settings.animations.player would return nil and the idle index is what causes the error.

One last thing, could you try printing the player table and player.idle before the error?

print(weaponTable.currentWeapon.settings.animations.player)
--> table: XXXXXXXX

print(weaponTable.currentWeapon.settings.animations.player.idle)
--> Animation

This will probably cause the last print up there to error.

It printed the table. … image

So I don’t know what you mean

1 Like

I think the error might be simpler than I thought, can you make sure weaponTable.loadedAnimations exists?

print(weaponTable.loadedAnimations)

If this prints nil, you should create the table first in whatever script is responsible for creating the weaponTable.

2 Likes

Thank you, works fine and dandy now.
image

1 Like