after linking to a .Changed function that gets and sets properties, I found this disturbing error, and I’m wondering, if a property isn’t scriptable, why does it even fire .Changed? this looks like it should be a hidden property anyway, and having no list of these ‘hidden properties’, how can I be expected to make a stable script combining these two features? my script would either become outdated, as new properties are added, should I check if it’s in a whitelist, or break later on should I check if the property is in a blacklist.
I know this is poor practice, but try using pcall() and check if it errors. I assume this is what you’re trying to do:
inst = script.Parent for _, v in pairs (inst:GetChildren()) do if not pcall(function () v.Changed:connect(function (property) --stuff end) end) then print(string.format("%s cannot be scripted.", v.Name)) end end
Your code would benefit from
[code]local isScriptable, value
isScriptable = pcall(function() value = obj[propname] end)
if isScriptable then …[/code]
XAXA - That won’t work. Pcall doesn’t magically protect everything inside of it, it just protects the code it runs. Your event function isn’t actually running inside the pcall, you’re passing it as an argument to connect, so the pcall won’t accomplish anything. The solution is to move the pcall inside of the event.
Events inside pcall do run. I checked in studio:
--inside a part pcall( function() script.Parent.Touched:connect(function(hit) print("Hello") -- prints "Hello" whenever it hits a part end) end)
EDIT: But yeah, it seems like you understood the problem better (he trying to change a locked property) Your solution is better.
I’m not saying pcall doesn’t run it, I’m saying pcall doesn’t protect it. Try to error in your Touched example. The error will go through.
It depends on what you’re trying to do. If, hypothetically, an instance doesn’t have .Changed as an event (it didn’t occur to me that .Changed is a member of every instance), a pcall() surrounding the event will protect it. What I thought Weeve was doing is that he was trying to iterate through a bunch of objects and trying to connect the .Changed event. But it’s clear to me (and the title which I completely misread) that he was trying to query a locked property.
Thanks, Arceus, but I compacted your code a hair to make it prettier
local IsScriptable = pcall(function() local _ = Part[Property] end) if IsScriptable then
moved the variable initialization to be the same line as the pcall, and removed the value since it was kinda useless, since I regard its existence as a ‘hack’, and would end up indexing the property afterwards anyways, which I would assume the same for most use cases
It caught another error, being that SpecificGravity cannot be set, so I’ve made another version that avoids that:
local IsScriptable = pcall(function() Part[Property] = Part[Property] end) if IsScriptable then