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

[Solved] Why do i get attempt to index nil with 'destroy'?

Asked by 4 years ago
Edited 3 years ago

script:

local isPressed = workspace.Bools.isButtonClicked

while true do
    wait()
    if isPressed then
        script.Parent:Destroy()
    end
end

workspace: https://imgur.com/a/8NCxjQR

Yep, exactly as the title says -.-

1
If you specify what script, this is I think I can help you. braydon12737 24 — 4y
0
thats the gate script EiOooAxea 70 — 4y
1
I don't know if I'm right, correct me if I'm wrong but I'm pretty sure it means "script.parent" is nil or don't exist. RainbowBeastYT 85 — 4y
0
its a server script raid6n 2196 — 4y
View all comments (2 more)
0
ah wait im dumb its a normal script EiOooAxea 70 — 4y
0
Firstly, make sure that once the instance you're trying to destroy is destroyed that you're breaking or stopping this loop. It may be the case that it is getting destroyed, and then you're getting this error trying to reference it again, saying that it not longer exists. Wure 5 — 4y

1 answer

Log in to vote
1
Answered by
Wure 5
4 years ago
Edited 4 years ago

Just a few things to point out...

local isPressed = workspace.Bools.isButtonClicked

This variable isPressed has been set to the BoolValue instance and NOT the actual value that the boolean instance may be.

To get the value of the boolean it would be this...

local isPressed = workspace.Bools.isButtonClicked.Value

This will mean that isPressed is either true or false now. What you had before meant that isPressed was always true, because the BoolValue instance existed and that's enough to pass as true.

Your script should look a little more something like this...


while wait(1) do local isPressed = workspace.Bools.isButtonClicked if isPressed and isPressed.Value then script.Parent:Destroy() return end end

I took the initiative to have this loop only run once a second. Depending on your situation you may not need an instantaneous response and a maximum second response may suffice. Just be sparing when it comes to using loops with wait(). Using a few is fine, but eventually they'll all add up! The additional return line is to break the loop so that after the instance is destroyed you're not forever still checking if the button was clicked.

Ad

Answer this question