Check if an instance in a table exists and return nil if not

I will have this module in a game where the instance will not exist, and to save trouble changing the package script, I want to check if an instance exists, if not return nil

Quest = {
	Objective = workspace.Rekt,
}

What I want to achieve is to check if an instance exists in a table, I have tried using

Quest = {
	Objective = workspace.Rekt or nil,
}

but that does not work and also tried using if and or one sentence but that also does not work

Thank you in advance,
Polo

workspace:FindFirstChild("Rekt")

“:FindFirstChild()” returns nil if the specified instance doesn’t exist anyway.

https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChild

Quest = {
	Objective = workspace.Rekt,
}

if not workspace.Rekt then
    table.find(Quest, "Objective") = nil
end

That should work, however, I don’t normally utilize tables, so correct me if I’m wrong! :slightly_smiling_face:

“Rekt” is not a valid member of workspace.

Would occur if there are no instances named “Rekt” inside the Workspace directory.

That’s what they want…

The title: “Check if an instance in a table exists, and return nil if not.”


On the same page, you never even told them to set the table to nil, you just provided a function that I’m sure they know of if they know tables. In addition, the if statement would return true and continue if it isn’t a member, meaning it would function perfectly fine.

“Rekt” is not a valid member of workspace.

Is an error, nil isn’t returned in that case, the script just abruptly stops.

My mistake, however, you still did provide them with a useless function. They weren’t asking for FindFirstChild, they were asking how they could set the table to nil if it couldn’t be found; Basically suggesting you provided them with half the answer.

The table doesn’t need to be set to nil. Take the following for example.

Quest = {
	Objective = workspace:FindFirstChild("Rekt") --is either a reference to an instance named "Rekt" inside workspace or nil
}

One other thing regarding your script.

table.find(Quest, "Objective")

The table library function “table.find()” is only valid for arrays not dictionaries. It’s used to find the index of some value in an array.

local array = {"a", "b", "c"}

print(table.find(array, "b")) --2

your solution worked perfectly ! I forgot you can use :findFirstChild