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

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

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

6 comments

1

If you specify what script, this is I think I can help you.

0

thats the gate script

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.

0

its a server script

0

ah wait im dumb its a normal script

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.

1 answer

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.

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