Hello, my name is Emi and I was wondering how I could do this?
As you can see CFrame is a function that several different inputs how can I replicate that in my code?
Hello, my name is Emi and I was wondering how I could do this?
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.new(item)
instance.Parent = parent
return instance
end
function Object.Destroy(item)
item:Destroy()
end
return Object
Then from another script, requiring the module
local Object = require(path.to.module) -- the variable's name can be anything
Object.New("Part", workspace)
Object.Destroy(workspace.Part)
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(arg)
end
end
Print(1,3,4,"abc") --> prints all of them
Go here: Programming in Lua : 5.2
What do you mean by that
Same variables is not good practice as it can override things
Like CFrame.new() 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
Example:
function CFrame.new(x)
local Vector3
if x then
Vector3 = x
else
Vector3 = (0, 0, 0)
end
return Vector3
end```
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. CFrame.new is implemented in C++ which supports function overloading, however Lua does not. You’ll need to manually write each case out in your function.