Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with 'Name' fix?

Asked by 3 years ago

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)

0
Please codeblock your code: select your code and then press the blue "lua" circle to codeblock it. It makes it easier to read. User#30567 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

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)
Ad

Answer this question