It’s a module that takes in a value, tries to find a -Value object that can hold it, then returns said object with its Value as the value that was given.
For example:
local Object = GetObjectFromValue(CFrame.new(6, 1, 2))
-- Object will be a CFrameValue with a Value of CFrame.new(6, 1, 2)
local Object_ = GetObjectFromValue("Apple")
-- Object_ will be a StringValue with a Value of "Apple"
I know it’s not much, but I hope I helped someone.
There’s some feedback I’d like to offer on this resource.
INST should be a local variable, not a global. You’re only using it in the context of the module and since it’s an upvalue it can be accessed by the returned function. This practice is fairly old anyway and IIRC accessing the speed of accessing standard libraries doesn’t differ from if you declared a function from one as a local variable.
The script is too long. It performs an unnecessary for loop through a table of ValueBase class names. Use what typeof returns to your advantage. You can also take a shortcut with the error by pcalling Instance.new and seeing if the object’s creation succeeds.
Here’s a rewrite I did of the module:
local typeRemap = {
["boolean"] = "Bool",
["Instance"] = "Object"
}
return function (value)
local valueType = typeRemap[typeof(value)] or typeof(value)
if not valueType then
warn(string.format("Unknown type for value %q", value))
return nil
end
local valueClass = string.gsub(valueType, "^%l", function(str) return string.upper(str) end) .. "Value"
local success, valueBase = pcall(Instance.new, valueClass)
if success and valueBase then
valueBase.Value = value
return valueBase
else
warn(valueClass .. " cannot be created")
return nil
end
end
typeRemap helps us to remap certain types based on what typeof returns. Since InstanceValue and BooleanValue do not exist, we remap those types to the appropriate ValueBase types.
We then want to check the type of the value with typeof, first through the remap and second through its raw type. If it doesn’t exist (shouldn’t hit this case), then warn in the console and return no object.
If we have a valueType, then we want the class to create. First, we make the first letter of valueType uppercase if it’s lowercase. Afterwards we append Value to the result to get in the naming convention of ValueBase objects, which is our valueClass variable.
Next we pcall Instance.new and pass valueClass to see if our class can be successfully created. If it can, then we set the Value of the object to the value passed originally and return the created object. If not, we warn the developer and return nothing.
In this case, we shift the more “expensive” part of the code from a loop to string manipulation and take advantage of things the engine already does to get an improved code sample.
If you wanted IntValue support then you can use math.modf if the valueType is a number and check if fractional > 0.
if valueType == "number" then
local fractional = select(2, math.modf(math.pi))
print(fractional == 0 and "Int" or "Number")
end