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

Players.GameBuilderLol.Backpack.9mm.Handle.DestroyTool:3: attempt to index nil with 'WaitForChild'?

So basically this error happens when i drop all of my ammo, i dont know what is causing this...

while wait() do
	wait(0.01)
	if script.Parent:WaitForChild("Ammo").Value <= 0 then
		script.Parent.Parent:Destroy()
	end
end

1 answer

✓ Accepted answer

Is a few questionable flaws in your script. First, the loop. It’s a while wait, but then you added another wait(0.01)? You could do while wait(0.01) if you needed. But in your case, this loop is completely unnecessary. Instead of having a loop, just check if you have no ammo every time the ammo value is changed. Lastly, your error. This occurs because the script has no time to wait for Ammo since you didn’t set it to a variable. Doing this should fix your errors. Here is a (hopefully) fixed version of your script. I’m currently on mobile so I couldn’t test it, though.

local ammo = script.Parent:WaitForChild("Ammo")
ammo.Changed:Connect(function(value)
    if value <= 0 then
        script.Parent.Parent:Destroy()
    end
end)

1 comment

0

Thank you it works!

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