Let’s say that we have a function that clones an instance in replicatedstorage and returns it. These are the function’s inputs and outputs.
Input: (instance_name: string) --the name of the instance to be copied
Output: (cloned_instance: Instance)
Is there a way to give the outputted instance a type that is somehow calculated properly? By that, I mean one instance in replicated_storage could hypothetically be a BasePart, but another is a TextButton. Is there a way to assign the correct types to the specific object through the output?
If anybody is confused, please let me know and I can try to rephrase.
Thanks for the reply. I think I used “type checking” wrongly. Let me try to use an example of how this would work.
local button = cloner.clone_instance("my_text_button")
button.Text = "Hello world"
^
|
The type checker gives “Text” as an optionable property since the cloned_instance output of the function somehow spits out the correct type. I’m really sorry, this is hard to explain.
No, as cloning an Instance could return any Instance. Your function will have to return the broad type of Instance and then your code which calls it will have to refine or cast the result afterwards.
e.g
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local function cloneReplicatedInstance(instanceName: string): Instance?
local instance = ReplicatedStorage[instanceName]
return if instance then instance:Clone() else nil
end
local newLight = cloneReplicatedInstance("Light") :: PointLight
Its not possible as studio will not look at the arguments you provided when type checking, the best thing you could do is to have separate functions for all the types.
If you want studio to be able to see the children of the instance you are trying to clone (here we are talking specifically if you are in studio editing, not in-game) you shouldn’t specify the type of the instance you are cloning, as when you do studio focuses only on that instance and ignores the children.
To add onto my other post, the closest to what you are trying to achieve would be something like:
cloner.clone_instance = {
["my_text_button"] = function()
local newInstance = script.TextButton:Clone()
newInstance.Parent = script.Parent.ScreenGui
return newInstance
end,
["my_text_label"] = function()
local newInstance = script.TextLabel:Clone()
newInstance.Parent = script.Parent.ScreenGui
return newInstance
end
}
local button = cloner.clone_instance["my_text_button"]()
This would work the way you want it to, however we are creating way too many functions for the simple task of cloning. To avoid using all those functions we could just have table of references:
However this wouldn’t be able to automatically parent the newly cloned instance somewhere, instead you would have to do it yourself. So simply put you can’t have both simpler process of cloning and not having to repeat yourself, as creating a function that will handle every instance removes the ability to type check, whilst other way you have to constantly type the same code.