So what I want to do is make a script that loops through a table, for every item I would want to tell if what it is, is a supported attribute type, likes strings, booleans, etc. I was just going to write out a table of all the types, but I thought I’d ask first so I dont have to waste time lol.
If there’s no other easy way I figure a hacky solution could be you try calling set attribute on some placeholder instance for every type and then wrap that call in a pcall so that if it catches an error you know the passed in type is invalid. Not a huge fan of using pcalls like this but it guarantees your code works, so if every other solution is too tedious this could be an acceptable method to use.
If you don’t want to premake a table of supported attribute types, you can go with @pullman45’s answer and simply cache the results.
--!strict
local VALID_TYPES: {[string]: boolean} = {}
return function<T>(value: T): boolean
if value == nil then
return true
end
local valueType: string = typeof(value)
if VALID_TYPES[valueType] == nil then
-- we don't have to worry about destroying
-- since this will get garbage collected anyways
local success: boolean = pcall(function(): ()
local instance: Instance = Instance.new("Folder")
instance:SetAttribute("_", value)
end)
VALID_TYPES[valueType] = success
end
return VALID_TYPES[valueType]
end
This should make sure that not only it’s the correct attribute type, but also it’ll still error if you’re doing something wrong that isn’t related to the type.
--!strict
local newInstance = Instance.new("Folder"):
local isCorrectType, info = pcall(function()
return newInstance:SetAttribute("Your attribute")
end
newInstance:Destroy()
if not isCorrectType and not info:Find("is not a supported attribute type") then
error(info)
end
If you really want you can make a function of it.
When using pcall for those kind of stuff I reccomend only disallowing certain errors, so in case you get an error that is different you won’t be confused as to what is wrong (Though here it will obviously not error but just for future practices)