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

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

Asked by 3 years ago

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

Log in to vote
1
Answered by
jundell 106
3 years ago

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)
0
Thank you it works! GameBuilderLol 58 — 3y
Ad

Answer this question