Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Return function? I have never seen something like this, please help!

Morning guys, so i was reading at some code of Ruddev's Battle Royale that is open source, and in Effects, there is a module script about shooting, inside, is a return function? i dont ever have seen something like this, so i ask u guys, does this return the function when i call require? or it has other behaviour? Thanks in advance! Also, let me show you.

return function(item, position, directions, ammo)

1 comment

0

In module scripts, module scripts return functions to a server script or local script, and then they can access the specific function. I don't know what the function does however, so that's all I can say.

1 answer

✓ Accepted answer

ModuleScripts can return anything, Instance, Boolean, String and of course a function! This module returns a function which you can easily call after requireing it, here, take this as an example:

ModuleScript

return function(Argument1, Argument2, ...)
	print(Argument1, 'is not', Argument2)
end)

Script (Local or Server)

local MyModuleFunction = require('Module script from previous code block')

MyModuleFunction('John', 'WideSeal')

Output: "John is not WideSeal"

It's that simple, just a normal function so it can also use return keyword:

ModuleScript 2

return function(Number)
	return Number * 2
end

Script2

local MyModuleFunction2 = require('ModuleScript 2')

local Result = MyModuleFunction2(50)

print(Result)

Output 2: "100"

Sources

Require

ModuleScript

ModuleScript as a function

This discussion is preserved as it was. New answers, comments, and votes are closed.