How do you go about returning functions in a Module Script?

So basically I want to know how to return with a module function, rather than a normal function. It doesn’t seem to operate the same when I try it. I’ve been looking around on the Devforum for a bit, but I can’t find any info.

Basically, I’m trying to do something like this.

-- Variables --
local Module = require(game.ServerStorage.RandoModule)

-- NORMAL FUNCTION --

local function Example()

	local test = "Hey, this is how you return, right?"
	return test
end

print (Example())

-- ^ This would print 'Hey, this is how you return, right?' . . At least I think it would. --

-- MODULE FUNCTION (PRETEND IT'S THE SAME, BUT IN A MODULE SCRIPT) --
Module.Example()

print (Module.Example()) ???

-- ^^ . . How do I return something like this? Add local behind it? . . It bugs out my script when I try

If someone could help, it would be appreciated :sweat_smile: :pray:

2 Likes

MODULE SCRIPT

return function()
    return "yes this is how you return!!!"
end

ANY SCRIPT

local Module = require(Path.To.Module)

print(Module())

return should work the same way, and yes you can return a function inside a module script

5 Likes

Functions belonging to ModuleScripts can return values just like any function of any script instance type.

1 Like

In fact while ModuleScripts are frequently used to return a single table value they can also be used to return a single value of any type (that includes but is not limited to a function value itself). In Lua, functions are recognised as first class values.

1 Like

I’m still having some trouble when I try doing this

print(Module.Hitbox())

But I’ll figure something out. It like tries to play the function inside of the print, and ends up with a bunch of errors, since there’s no parameters in there

If the function receives parameters (of which values are expected for) then you should try:

print(Module.Hitbox(arg1, arg2, arg3...))
1 Like

so i was wondering how to make function call itself idk if i said it right i don’t really know what it suppose to be called its like humanoid when the the character stops walking

local hum = script.Parent:FindFirstChild("Humanoid")

hum.MoveToFinished:Connect(function()
	--rest of code
end)

like this but in module script