Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index nil with 'Name' What does this mean?

local function partTouched(onTouch)
	local tool = game.Players:FindFirstChild("Tool")
	if tool.Name == "Sword" then
		tool:Destroy()
	end
end

script.Parent.Touched:Connect(partTouched)

I made this script to remove your sword when you touch this part It doesn't work for some reason and here is the output error:

11:30:44.415 - Workspace.Part.Script:3: attempt to index nil with 'Name'

9 comments

0

are you sure there is a tool in players?

0

yes i put a tool in starterpack which should mean theres one in the backpack of the player

0

then you do local tool = game.Palyers.Backpack:FindFirstChild("Tool")

0

players*

0

i dont think you understand where the backpack is

0

script.Parent.Touched:connect(function(p) if p.Parent.Name == "Key" then p.Parent:Destroy() end end)

0

bruh backpack is in the localplayer that is in players

0

That can do it if they are holding the tool.

0

yes but you cant say game.Players.backpack lol

2 answers

✓ Accepted answer

In your case, it means that "tool" does not exist. Also, tools go inside the player's backpack, not inside the player object. I assume your code should look something like this below.

local function partTouched(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player then
		local tool = player.Backpack:FindFirstChild("Sword")
		if tool then
			 tool:Destroy()
		end
	end
end

script.Parent.Touched:Connect(partTouched)

It means your trying to check for a name and trying to change the name to that specific name to fix this try:


local function partTouched(onTouch)
local player = game.Players.LocalPlayer
  local Tool = player.Backpack:FindFirstChild("Tool") --Find the tool name
   if Tool then
if Tool.Name == "Sword" then
tool:Destroy()
end
end
end
end
script.Parent.Touched:Connect(partTouched)

8 comments

0

so much errors in your code bruh

0

Im on mobile.

0

so? mobile users can type well too

0

Whats wrong with it?

0

A backpack is not inside the player service, it is inside the local player.

0

Like I said Im on mobile so its hard for me to check for errors.

0

Ofc i know that

0

You are checking for a child of backpack with the name "Tool" and then you are checking if the child's name is "Sword"?

This discussion is preserved as it was. New answers, comments, and votes are closed.