User:NXTBoy/Scripts/Configuration
From Legacy Roblox Wiki
< User:NXTBoy | Scripts
local hasProperty = function(object, property)
return pcall(function() return object[property] end)
end
local createWrapperObj = function(value)
local objectType = nil
if type(value) == "string" then
objectType = "StringValue"
elseif type(value) == "boolean" then
objectType = "BooleanValue"
elseif type(value) == "number" then
if math.floor(value) == value then
objectType = "IntValue"
else
objectType = "NumberValue"
end
elseif type(value) == "userdata" then
if hasProperty(value, "lookVector") then
objectType = "CFrameValue"
elseif hasProperty(value, "z") then
objectType = "Vector3Value"
elseif hasProperty(value, "Origin") then
objectType = "RayValue"
elseif hasProperty(value, "className") then
objectType = "ObjectValue"
elseif hasProperty(value, "Color") then
objectType = "BrickColorValue"
elseif hasProperty(value, "r") then
objectType = "Color3Value"
end
end
local wrapper = nil
pcall(function()
wrapper = Instance.new(objectType)
wrapper.Value = value
end)
if not wrapper then
wrapper = Instance.new("StringValue")
wrapper.Value = tostring(value)
end
return wrapper
end
local findConfig = function(part)
if not part:isA("Configuration") then
for _, child in ipairs(part:GetChildren()) do
if child:isA("Configuration") then
return child
end
end
end
return part
end
_G.Configuration = function(part, defaults)
defaults = defaults or {}
local configWrapper = findConfig(part)
return setmetatable({}, {
__index = function(t, name)
local camelName = name:gsub("^%l", string.upper)
local configValue = configWrapper:FindFirstChild(camelName)
if configValue then
return configValue.Value
else
return defaults[name]
end
end,
__newindex = function(t, name, value)
local camelName = name:gsub("^%l", string.upper)
local configValue = configWrapper:FindFirstChild(camelName)
if configValue then
if not pcall(function() configValue.Value = value end) then
print("Warning: type of configuration value "..camelName.." has been changed!")
configValue:remove()
else
return
end
end
local obj = createWrapperObj(value)
obj.Name = camelName
obj.Parent = configWrapper
end
})
end