Hey guys im not sure if i’m on the right topic let me know if i am not.
I’ve been analyzing this code in the classic sword script and i saw return function(data) when there was no other function in that script or any other one so what does return function(data) mean?
Here’s a very good post that goes into detail on return:
In short, it’s main use is to, as the name implies, return some sort of value. Though, it can be used as a way to terminate a function as well. After looking at the code you’re referring to though, it is used to return a function that the Create function creates.
function Create(ty)
return function(data)
local obj = Instance.new(ty)
for k, v in pairs(data) do
if type(k) == 'number' then
v.Parent = obj
else
obj[k] = v
end
end
return obj
end
end
The Create function is called upon in this line of code:
local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
Name = "R15Slash",
AnimationId = BaseUrl .. Animations.R15Slash,
Parent = Tool
})
The ty parameter is to the "Animation" string, while the brackets indicates the data (it’s a table).
-- Visually, that looks like this:
Create(ty)(data)
You can also attach a table and grab info from that. Everything from strings, to more tables, or even functions or threads.
Formatting can get pretty crazy.
function SomeFunction(ReturnVal, call)
if ReturnVal then
return function(Tab)
if not table.find(Tab, ReturnVal) then
warn("Value not found in extra table!")
return nil
end
return if call then Tab[ReturnVal]() else Tab[ReturnVal]
end
else
print("Ran with no added table.")
end
end
SomeFunction()
-- prints "Ran with no added table."
local Apple = "A red apple!"
local Banana = {Color = "Yellow", Name = "Banana"}
local function Lemon()
print("A sour, yellow lemon!")
end
print(SomeFunction("Apple"){
["Apple"] = Apple;
["Banana"] = Banana;
["Lemon"] = Lemon;
})
print(SomeFunction("Banana"){
["Apple"] = Apple;
["Banana"] = Banana;
["Lemon"] = Lemon;
})
SomeFunction("Lemon", true){
["Apple"] = Apple;
["Banana"] = Banana;
["Lemon"] = Lemon;
}
It’s definitely not advisable, but you could take it a couple steps further and practically just emulate other languages like Javascript and C++ if you structure your code properly lol
Keep in mind that for certain functions/events, returning values such as deep/mixed tables may not give you the expected result (instances such as RemoteEvents come to mind)