How to determine if a property is read-only via ReflectionService:GetPropertiesOfClass?

Hello, I made a system that dynamically saves properties to a datastore, but It doesn’t take read-only properties into account.

This is what I have currently, but I don’t 100% understand how SecurityCapabilites work, and according to another user, this is incorrect:

--[[
Checks if a property is read-only for a standard script
@param className string
@param propertyName string
@return boolean?
]]
local function isPropertyReadOnly(className: string, propertyName: string): boolean?
	local properties = ReflectionService:GetPropertiesOfClass(className, {
		Security = allCapabilities
	})

	if not properties then
		warn(`Class {className} not found.`)
		return nil
	end

	for _, propInfo in properties do
		if propInfo.Name == propertyName then
			
			local writePermit = propInfo.Permits.Write
			
			-- It is read-only ONLY if the permit exists (not nil)
			-- AND that permit is not 0 (not "None").
			return writePermit ~= nil and writePermit ~= 0
		end
	end

	warn(`Property '{propertyName}' not found on class '{className}'`)
	return nil
end

What’s the proper way to determine if a property is read-only via ReflectionService?

I have an idea of using pcall to try and change the value of every property, and in catch part you define that one as read only because catch part will catch the error (Trying to change value in a read only property).

I’m avoiding doing that, it’s hacky

This isn’t exactly how the API dump is formatted but I think it has the same features.

From here I’m guessing if there isn’t a “Write” in the “Permits” then you can’t write that value, aka being Read-Only.

This seems like the case for all values that are read-only.


First member is writable, but the second member that is opened is not.


To check for this may be similar to

local CanWrite = false
if Permits["Write"] then 
    CanWrite = true 
end

Here’s a simplified version:

local reflectionService = game:GetService('ReflectionService')
local data = reflectionService:GetPropertiesOfClass(class)
for _,p in data do
	if p.Permits.Read and p.Display.Category ~= 'Permissions' and not p.Display.DeprecationMessage then
		if p.Permits.Write then
			-- Writable
		else
			-- Read-Only
		end
	end
end

Note that this will still include some properties that should be Hidden. I’ve created a list of Hidden properties, but there are still properties in that list of which shouldn’t be Hidden. I generated the list from the Roblox API dump.


Unfortunately, it looks like there won’t be a native solution for a long while. So a manual list will be needed until then:

1 Like