Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this error on me? [pcall]

I was looking through the function dump page, and saw pcall. When I tried testing it as so, I get 'Baseplate' is not a valid member of Workspace

Don't think I'm misspelling anything wrong. When I put in BasePlate, it prints yes

I'm trying to test the condition with an object that doesn't exist

oExists = function(thing)
local success,msg = pcall(function() return thing ~= nil end)
if success then
	return true
end
return false
end
print(oExists(workspace["Baseplate"]) and "yes" or "no")

1 comment

0

pcall is almost never the answer. In particular, just reading this code, the function pcall is given can't ever error.

1 answer

✓ Accepted answer

The problem is this part:

workspace["Baseplate"]

If there is no object named "Baseplate" in Workspace, that statement will error, as you are attempting to reference a nil value outside of the protected call. To make this function work as you would like it to, you could do something like this:


local oExists = function(directory, thing)

	local success = pcall(function() return not not directory[thing] end)
	
	return success

end

print(oExists(workspace, "Baseplate") and "yes" or "no")

But honestly, you could just use directory:FindFirstChild(string object_name), which is much easier.

This discussion is preserved as it was. New answers, comments, and votes are closed.