GetDescendants returns nil

Hi, does anyone know why this returns with Workspace.AutomaticDoor.Configuration.Controller:87: attempt to index nil with ‘Connect’

Any help is appreciated!

local locked = Tablets:GetDescendants("Locked")
locked.MouseButton1Down:Connect(function()
	if mode ~= 1 then
		mode = 1
	elseif mode == 1 then
		mode = 0
	end	
end)

I think you mean to use FindFirstDescendant not :GetDescendants as it returns a table of descendants.

local locked = Tablets:FindFirstDescendant("Locked")
locked.MouseButton1Down:Connect(function()
	if mode ~= 1 then
		mode = 1
	elseif mode == 1 then
		mode = 0
	end	
end)

I have multiple of the button though, will that still work?

local locked = Tablets:FindFirstChild("Locked", true)
locked.MouseButton1Down:Connect(function()
	if mode ~= 1 then
		mode = 1
	elseif mode == 1 then
		mode = 0
	end	
end)

Oh, then I think you want to do this:

for i,locked in pairs(Tablets:GetDescendants()) do
	if locked.Name == "Locked" then
		locked.MouseButton1Down:Connect(function()
			if mode ~= 1 then
				mode = 1
			elseif mode == 1 then
				mode = 0
			end	
		end)
	end
end
2 Likes

FindFirstDescendant() isn’t available yet, you’ll see a warning when you attempt to use it.

This is the correct solution, providing the instance is named “Locked” as the thread’s poster stated.

locked.MouseButton1Down:Connect(function()
	mode = if mode == 1 then 0 else 1
end)

Ah, I didn’t know that it wasn’t available yet, I haven’t tried to use it but I knew it was a thing. Thanks!

This works, however, my game is crashing from script timeout :thinking: ive added a wait and it still crashes its only a tween how strange, anyways ty for the help!

There isn’t any tweening occurring in the script you provided.

1 Like

I haven’t provided the full one.

You should probably add TweenName.Completed:Wait() wherever you are playing tweens. Change “TweenName” to the name of the variable of which the created tween is assigned to.

1 Like

I’ll try that :smiley: , thank you for the help!