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

How do I check if an instance has a certain property?

Title says it all

To explain:

local property = "Name"; -- Name is a property every instance has.

for i,v in next, workspace:GetChildren() do
	if (v[property]) then
		print("found: "..tostring(v)..";\n    property: "..tostring(property));
	end
end

The above doesn't error because every instance in workspace has a property "Name"...

However:

Instance.new("StringValue", workspace);

local property = "Value"; -- Not every instance has this property 

for i,v in next, workspace:GetChildren() do
	if (v[property]) then
		print("found: "..tostring(v)..";\n    property: "..tostring(property));
	end
end

The above errors saying that "Value" is not a valid property of camera? Why does it error? I clearly state that IF the instance has a property of "Value", then to print. It completely ignores it.

1 comment

0

Man, the way you're using lua...it looks like another language. This is totally beyond me.

1 answer

✓ Accepted answer

I'm not certain why that would be erroring, but you can use pcall to catch any invalid accesses:

for i, v in next, workspace:GetChildren() do
	if pcall(function() v[property] end) then
		print("found: " .. tostring(v) .. ";\n	property: " .. tostring(property));
	end
end

1 comment

0

I realised that, thanks anyway!

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