GetDescendants is a nil value

ModuleScript

local RS = game:GetService("ReplicatedStorage")
local GS = game:GetService("GuiService")
local UIS = game:GetService("UserInputService")

local module = {}

--Set selection group
function module.SelectionGroupToggle(parentObj, bool)
	--GS:AddSelectionParent(name, parentObj), this is apparently glitchy/broken since 2015 so no thanks
		
	local descendants = parentObj:GetDescendants() --here's where it throws the error
	for index, descendant in pairs(descendants) do
		if descendant:IsA("GuiButton") then
			descendant.Selectable = bool
		end
	end
	return true
end

return module

LocalScript

local KB = require(script:WaitForChild("KeybindModule"))
local introFrame = script.Parent:WaitForChild("IntroFrame")

KB:SelectionGroupToggle(introFrame, false)

KeybindModule:11: attempt to call method 'GetDescendants' (a nil value)

Why does it seem to think my introFrame object is a nil value?

In the LocalScript, printing the name of the object returns a value. Printing it in the module returns nil.

2 Likes

It’s because of the way you’re calling it.

The module adds the function in with a dot but you’re calling the function with a colon. The module is being passed as the implicit first argument, self, so the arguments that SelectionGroupToggle is receiving is actually self, parentObj, bool. Bool is discarded because there isn’t a third parameter for the argument to be assigned to.

You can fix this by either adding the function to the table with a colon as well, adding self as the first parameter to SelectionGroupToggle or changing the LocalScript to call the function with a dot.

-- Any of these three work. Do not apply all of them, choose one.

function module:SelectionGroupToggle(parentObj, bool)

function module.SelectionGroupToggle(self, parentObj, bool)

KB.SelectionGroupToggle(introFrame, false)
3 Likes

Thank you very much for the clear and thorough explanation.