How do you simulate Instance:Clone() Intellisense behavior?

Just a general question.

  • newPart:Clone(), a Roblox object has autocomplete.
  • newObject:Clone(), a custom object doesn’t have autocomplete .
  • How do I make the custom :Clone() function similar to roblox’s :Clone()?

intellisense prob1

  • newPart is a cloned object, its autocomplete is similar to part

intellisense prob2

  • someObject is an instanced object. It has autocomplete.
  • newObject is a cloned object, it has the type: unknown

intellisense prob3

Try typing in --!strict at the start of your code, or create a type reference for your table:

--!strict

type _someObject = {
    Name: string
}

local module = {}
module.__index = module
function module.new()
  local self: _someObject = setmetatable({}, module)

  self.Name = "asgm"
  
  return self
end
function module:Clone()
  local new = module.new()
  for k, v in pairs(self) do
    new [k] = v
  end
  return new
end

local someObject = module.new()
local newObject = someObject:Clone()
someObject.Name = "asgm"

I don’t have studio with me right now so I have no way of testing this out.

Hmm I was hoping that there was a less time-consuming solution : /

I did think of that, but I’m just too lazy to retype every method inside the type _someObject especially when the object starts getting more methods.

I think I’ll leave the code as it is since the :Clone() part is only like 10% of the code I’ll use.

Thanks for the help though

If you are using it a lot just copy and paste, changing the variables themselves so that it autofills would be more time consuming the writing it oit

The only alternative would be to use a function to return a clone of whatever you put in it and instance it