Replicating Instance.new() autocomplete

Hi!
I’m currently working on a game, and I started to notice that I was creating more and more classes, so I wanted to write something like Instance.new() for my own classes. It works just fine, but the problem is that the autocomplete for instances created like this is non-existent. I was able to create a type for the string names of the classes to use as a parameter in my function, but I’m just wondering how to let roblox intellisense know that I’ve created an instance of a certain class and have the corresponding autocomplete. Here’s my code:

local InstanceTypes = require(script.Parent._InstanceTypes)

type rxscriptClassName = "DataStoreSettings" | "Signal"

export type rxscriptClass = InstanceTypes.DataStoreSettings | InstanceTypes.Signal

return function(ClassName: rxscriptClassName, ...) : rxscriptClass
	if not InstanceTypes[ClassName] then error("Please enter a valid class name") end
	
	return InstanceTypes[ClassName](...)
end
1 Like

Maybe try checking the ScriptEditorService API? It has that RegisterAutocompleteCallback API, haven’t look in depth, if you made a plugin to integrate it on the auto complete it could work, if it really works for Instance.new, otherwise there wouldn’t be much you could do I guess

I’m not trying to make a plugin here though. I’m trying to combine the constructors for multiple classes into a singular function call. When I create instances of those classes through the constructors in their modules, autocomplete works just fine. The problem is that when I combine these into a function call, the autocomplete just goes away. I know that this is because there’s no easy way for the intellisense to know what I’m creating, but I just wonder if there’s some way to get auto complete to work.

Might work

local InstanceTypes = require(script.Parent._InstanceTypes)

type dataStoreSettings = ("DataStoreSettings", ...any) -> InstanceTypes.DataStoreSettings
type signal = ("Signal", ...any) -> InstanceTypes.Signal
type constructor = dataStoreSettings & signal

return function(className, ...)
	if not InstanceTypes[className] then error("Please enter a valid class name") end
	
	return InstanceTypes[className](...)
end :: constructor
1 Like

It actually worked, thank you so much! I’ve always wondered how Roblox did function overloads since there aren’t any implemented into Lua, but I guess this is how they do it. Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.