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.
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