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

How to have module return 2 values?

function module()
	if (redPlayers <= 0) and (bluePlayers > 0) then
		gameEnd = true
		return game.Teams.Blue
	elseif (bluePlayers <= 0) and (redPlayers > 0) then
		gameEnd = true
		return game.Teams.Red
	elseif (redPlayers <= 0) and (bluePlayers <= 0) then
		gameEnd = true
		return nil
	end
end

module()

What I'm trying to do is when this module is called, it returns who the winning team is, after the round is over. How can I get it to return who wins?

1 answer

✓ Accepted answer

You'll have to return the function as {Value = Value}. When you then call the function via module.FunctionName(), it will return whatever you tell the function to return (Or execute stuff)

Example:

In the module script:

--Module
function CalcPlus(Num1,Num2)
	return (Num1 + Num2)
end
function CalcMin(Num1,Num2)
	return (Num1 - Num2)
end

return { -- <- this is the return that your error comes from. If you don't have this return, the script won't even be able to do anything.
	CalcPlus = CalcPlus,
	CalcMin = CalcMin
}

We can use this in a script as follows:

--Script
local Module = require(TheModule)

print(Module.CalcPlus(4,6)) -> "10"
print(Module.CalcMin(4,1)) -> "3"

For your problem, we would remove the Module() call, and replace it by

return {module = module}

2 comments

0

I edited my scrip to what I have now. But it still keeps saying I can't return more than 1 value, even though I'm only returning 1 value.

0

What exactly do you return? (This error comes from the return closest to )

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