Attempt to index number with 'Name'

Hi, all. This script is throwing the error “attempt to index number with ‘name’”. The purpose of the script is to detect a specific type of magazine in the player’s inventory and then change the magazine count in the actual firearm to +1 for each magazine detected.

while true do
	wait(1)
	
	if script.Parent.Parent:FindFirstChild("Humanoid") then
		local Player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)
				
		for i,v in pairs(Player.Backpack:GetChildren()) do
			if i.Name == "9mm" then
				script.Parent.Mags.Value = script.Parent.Mags.Value + 1
			end
		end
	end
end

Thanks for your help in advance.

1 Like

The error means that you have mistaken i and v in a for pairs loop. For GetChildren(), it returns an array and sets i to the index (place number) and v to the object (value) you’re looking for.
All you need to do is change i.Name to v.Name and you’re done

2 Likes

If you use an in pairs loop, the i is the index of the table you’re looping through. The index is basically the number of the item you’re (or the table) is dealing with. (e.g. the first item’s index is 1, the second’s 2, etc. Basically their order.)

I’m pretty sure you accidentally put i instead of v when you indexed the name.
You can also memorize their roles by knowing that
i stands for index,
and v stands for value.
(I should also mention that these letters are variables, so you can change them to anything you want. But their purpose never changes)

TL;DR: Use v instead of i.

Ohh, right! Thanks so much for this. I didn’t even think about that, lol.

Perfect! Thanks for the information, as well. I didn’t even think about that!

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