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

Attempt to index nil with 'Name' fix?

Im trying to make a tool that removes a specific model when they collide with each other but I keep getting this error message in the output. How can I fix this?

local tool = script.Parent local handle = tool:WaitForChild("Handle") local w = workspace:GetChildren()

tool.Activated:Connect(function() local str = Instance.new("StringValue") str.Name = "toolanim" str.Value = "Slash" str.Parent = tool wait(2) end)

handle.Touched:Connect(function(hit) if w[hit].Name == "mushroom 1" then w[hit]:Remove() wait(30) end end)

1 comment

0

Please codeblock your code: select your code and then press the blue "lua" circle to codeblock it. It makes it easier to read.

1 answer

The problem lies in the following line:

handle.Touched:Connect(function(hit) 
	if w[hit].Name == "mushroom 1" then 
		w[hit]:Remove() 
		wait(30) 
	end 
end)

Short answer: w[hit] doesn't exist.

Long answer: The hit argument is an Instance, whereas the GetChildren returns a numbered array.

You have to replace w[hit] with just hit. Again, I'm not sure this will work, because I don't have the full context.

Try this:

handle.Touched:Connect(function(hit) 
	if hit.Name == "mushroom 1" then 
		hit:Remove() 
		wait(30) 
	end 
end)

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