GetPlayerFromCharacter "attempt to index nil with Backpack"

Hey all, I’m trying to make a metal detector that gets the player’s backpack and checks for something that identifies an unauthorized tool.

However, when I fire the function, the console returns this:

(tree stuff): attempt to index nil with ‘Backpack’

This is the code I currently have that fires the error:

detector.Touched:Connect(function(Touched)
	for i,v in pairs(game:GetService("Players"):GetPlayerFromCharacter(Touched.Parent).Backpack:GetChildren()) do
		if v.Name == "meat" then
		-- stuff here
		end
	end
end)

I have already pored over old posts and Developer Hub articles, and haven’t found anything to help yet. I have also tried using variables to the same if not worse effect.

I’m a brand new, straight out of the boat scripter, so any help would be appreciated. Thank you!

5 Likes

This means that there is no player associated with the touched part’s parent. Just check if the value of GetPlayerFromCharacter is not equal to nil.

5 Likes

Oh wow, that worked! Thank you for saving me an extra hour of facepalms!

2 Likes