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