How can I account if something is nil and not have the script error?

So I saw this other script part of a data store

 	local savedValues = DataStore:GetAsync("FolderValues") or "New"
	if savedValues == "New"  then
print("new")
else
print("existing")
end

How come this works ^

But this will error if it doesn’t exist:

local graves = workspace:WaitForChild("Graves")

function updateGrave(player)
	local whichGrave = graves[player.Name] or "New"
	if whichGrave == "New" then
print("new")
else
print("old")
end
end

updateGrave(player) -- error -> matt1020304050 is not a valid member of Folder "Workspace.Graves"
1 Like

It must be because where it says:

local whichGrave = graves[player.Name] or "New"

You are trying to locate the player character inside the Grave model. Player Characters are located inside of Workspace.

1 Like

Nah, I named the grave to the player’s name. I’m just trying to see if a grave owned by the player exists. I ended up fixing this using a pcall function, but curious why this doesn’t work.

1 Like

Because in the above statement the result of GetAsync returns a tuple of 2 nil values when key “FolderValues” key does not exist so nil or “New” would result in “New”. In your second example, graves[player.Name] you are trying to access workspace.Graves.matt1020304050 which is not a valid member and is throwing an error. You most likely want this instead:

local whichGrave = graves:FindFirstChild(player.Name) or "New"
1 Like

Isn’t using [] the same as using FindFirstChild() ?

1 Like

No using [] is equivalent to just using a period. The difference is certain things like children whose name start with numbers or contains spaces won’t work with a period and you must use the bracket and quotes.

1 Like

Strange, I thought I read a long time ago that it was.

So no matter what using FindFirstChild() and if it’s nil, it wont return an error?

I swear I’ve done this before and still ran into errors. In the past I always had to do this:

if not graves:FindFirstChild(player.Name) then return end – only way it didn’t return an error.

Is the only difference between [] and FFC() that FFC wont return an error if null?

1 Like

Here is the documentation on FindFirstChild:

Returns the first child of the Instance found with the given name. If no child exists with the given name, this function returns nil

I wouldn’t say the only difference as [] is also used to index tables with numbers as well.

1 Like

Thats a folder not a table, if you want to handle it as a table:

local graves = workspace:WaitForChild("Graves"):GetChildren() -- now is a table not a folder
local whichGrave = graves[player.Name] or "New"

If you want to handle it as a folder:

local graves = workspace:WaitForChild("Graves") -- folder
local whichGrave = graves:FindFirstChild(player.Name) or "New"

This is actually not correct. In your first example graves would be a table that is an array and you would need to index it with numbers not strings. Accessing with a string would always result in a nil.

1 Like

Lol, yeah thats true, I was thinking on a dictionary not an array, hahah sorry I had a brain glitch for a moment. :crazy_face:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.