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 4 years ago

script:

1local isPressed = workspace.Bools.isButtonClicked
2 
3while true do
4    wait()
5    if isPressed then
6        script.Parent:Destroy()
7    end
8end

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...

1local 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...

1local 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...

1while wait(1) do
2    local isPressed = workspace.Bools.isButtonClicked
3 
4     if isPressed and isPressed.Value then
5        script.Parent:Destroy()
6        return
7    end
8end

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