Here, some pseudo-code:
Vararg:
local function printCharName(...)
local args = {...}
local Char = args[1]
print(`Character name is {Char.Name}`
end
printCharName(game.Players.LocalPlayer.Character) -- prints ur char name
Arg table:
local function printCharName(args)
local Char = args.Char
print(`Character name is {Char.Name}`
end
printCharName({Char = game.Players.LocalPlayer.Character}) -- prints ur char name
Basically the same result. Varargs should be used only when you don’t know any of the arguments, or you can use vararg as an extension to known arguments, like:
local function applyCharStats(Char: Model, ...)
local extraArgs = {...}
print(`Character name is {Char.Name}`)
for _, Data in extraArgs do
if typeof(Data) == "table" then
if Data.Speed then
-- apply some calculations etc
end
if Data.Damage then
-- apply some calculations etc
end
if Data.Trait then
-- apply trait
end
end
end
end
applyCharStats(game.Players.LocalPlayer.Character, {Speed = 10, Damage = 20}, {Trait = "Brave"})
That’s if we don’t specifically know arguments - we had to parse them and search.
But we can follow a different approach, when we specifically know what to do:
-- we can use a switch case for diff stats
local StatCallbacks = {
Speed = function(Char: Model, Value: Number)
-- do some calcs and apply
end,
Damage = function(Char: Model, Value: Number)
-- do some calcs and apply
end,
Trait = function(Char: Model, Name: string)
-- apply trait
end,
}
local function applyCharStats(Char: Model, Config)
print(`Character name is {Char.Name}`)
for Stat, Value in Config do
if StatCallbacks[Stat] then
StatCallbacks[Stat](Char, Value)
end
end
end
applyCharStats(game.Players.LocalPlayer.Character, {Speed = 10, Damage = 20, Trait = "Brave"})
As you can see, we’ve achieved the same goal without varargs and improved the code as well.