Module function problem

Alright so I’ve got a module script and a server script
I’m trying to return a certain output of a function from it but that seems to be not possible

Module script:

local function smile(coolstring)
	return coolstring.." :)"
end

script

local module = game.ReplicatedStorage.MyModule --MyModule is the module script
local module2 = require(module)
print(module2.smile("This is a text"))

It gives me a weird error saying I’m returning more than one value, which is odd because I’m clearly returning only one

expected output:
“This is a text :)”

1 Like

Is this your entire module script? If so, you aren’t formatting it correctly. You need to literally return a value from the module itself, outside of a function.

Most often people format modules like:

local module = {}

function module.smile(coolstring)
    return coolstring .. ' :)'
end

return module

then

local module = require(replicatedStorage.module)
print(module.smile('this is a text'))

However you can also just return a function like so:

local function smile(coolstring)
    return coolstring .. ' :)'
end

return smile

then define smile as module

local smile = require(replicatedStorage.module)
print(smile('this is a text'))

It’s just whatever is the return result of the module’s source itself is what needs to be returned. Require doesn’t just dump the environment of the module script’s source into a table.

3 Likes

Woah, how come I never thought about this?
I’ve always tried returning the string and not the function

Thanks a lot!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.