Function with different arguments

Hello, my name is Emi and I was wondering how I could do this?image image

As you can see CFrame is a function that several different inputs how can I replicate that in my code?

You can use modules for this purpose.

Here is an example:

Create a new module in ServerScriptService,
inside it use this code:

local Object = {}

  function  Object.New(item, parent)
        local instance = 
        instance.Parent = parent
        return instance

  function Object.Destroy(item)

return Object

Then from another script, requiring the module

  local Object = require( -- the variable's name can be anything
  Object.New("Part", workspace)

Sadly thats not what I am asking. The thing I am trying to achive is to have the same variable but with different variables going into to it like CFrame.New() that can have 6 different inputs.

Agree if he uses modules but he is using CFrame functions instead

oh, sorry

you can make functions with a variable number of arguments like this:

pass a parameter as (three dots, no less)

  function Print(...) -- you can event pass a parameter before ..., but not after it
           for _, arg in ipairs({...}) do 

 Print(1,3,4,"abc") --> prints all of them

Go here:

What do you mean by that

Same variables is not good practice as it can override things


Like I am trying to make a constructor. As seen in the image above it takes in different arguments and does different stuff based on the arguments.

You have to make it so that it checks if there’s an argument passed

  local Vector3
  if x then
    Vector3 = x
    Vector3 = (0, 0, 0)
  return Vector3

I am trying to achieve something like this in Lua. (This code is C#)

What you’re talking about is known as function overloading. It is where a single function can do different things based on its arguments. is implemented in C++ which supports function overloading, however Lua does not. You’ll need to manually write each case out in your function.