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

Is this the right way to return and get things from a module script?

ServerScript

local Mod = require(game.Workspace.ModuleScript)



 game.Workspace.Value.Value = Mod.Return()

Module_Script

local module = {}

function Return()
	
	return 40
end


return module

game.Workspace.Value is a intvalue and its value isnt changing to 40 for some reason

2 answers

✓ Accepted answer

Ah, you were close. Basically, a module is a table. How do we add things into tables? Well we can do it a number of ways. You can use dot syntax, brackets, etc.

So if we have a table called module (module = {}), then we need to add a function into that table.

You do that by doing function module.Func() end

ServerScript:

--Script
local module = require(workspace.ModuleScript) --grab the module

local numValue = game.Workspace.NumValue 
numValue.Value = module.Return() --return function returns 40, value is now equal to 40

ModuleScript:

--Module

local module = {} --Table

function module.Return() --Add function to table
	return 40
end

return module --return the module 

If you want a bit deeper understanding, then you should also know that we're using syntax sugar whenever we do function module.Func() end.

Normally you'd have to do something like this: module.Func = function() end but luckily we have something thats much easier to read now.

3 comments

0

My wordings a bit off, a module isnt exactly a table* it returns a table with whatever you put in it. Sorry about that, but everything else is more or less correct here. I wont bother changing it

1

Hey they don't have to return a table but this is correct apart from that

0

Thanks haha.

You're still not doing what I told you to. Use

local module = {}

function module.Return()
	return 40
end

return module

2 comments

0

you should actually explain why, instead of throwing code in peoples faces, dude.

0

I did on the post he posted literally 10 mins ago

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