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

Why does my script think I'm attempting to index a nil value?

My script is supposed to "open and close" a door using ReplicatedStorage. When the server starts, there is one closed door (door1) already there, and door1 and door2 in ReplicatedStorage.

The script works to open the door, but when I click on the opened door (door2) to close it, I get the error:

Workspace.Script:15: attempt to index a nil value

function opendoor()
	game.Workspace:WaitForChild("door1")
	local door1 = game.Workspace:FindFirstChild("door1")
	door1:Destroy()
	game.ReplicatedStorage.door2:Clone().Parent = workspace
end

function closedoor()
	game.Workspace:WaitForChild("door2")
	local door2 = game.Workspace:FindFirstChild("door2")
	door2:Destroy()
	game.ReplicatedStorage.door1:Clone().Parent = workspace
end

game.Workspace:WaitForChild("door1")
game.Workspace:FindFirstChild("door1").ClickDetector.MouseClick:connect(opendoor)

game.Workspace:WaitForChild("door2")
game.Workspace:FindFirstChild("door2").ClickDetector.MouseClick:connect(closedoor)


Note: the script gets cut off above.

..."connect(opendoor)"

..."connect(closedoor)"

1 comment

0

If that's the only error you're receiving, then it most likely has to do with your RBXScriptSignal on Line 15. The script is either not finding: the door2 or the ClickDetector. Make sure that they both exist when you click.

1 answer

game.Workspace.door2 doesn't exist.

Is this a LocalScript? You'd do well to use WaitForChild. Same goes for if door2 is being made by a script, as it would appear to be here. You'll need to connect to the event when the door is made, and not before.

2 comments

0

It's not a localscript. If it were, where would I put it? I wasn't quite sure where to put the WaitForChild, but I made an educated guess. I edited the question. Could you check if I used it correctly? Also, now I can close door2, but can't open door1 again.

0

You don't connect it again.

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