Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
2
Answered by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years ago

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.

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 Psudar 882 — 4y
1
Hey they don't have to return a table but this is correct apart from that User#24403 69 — 4y
0
Thanks haha. Psudar 882 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

local module = {}

function module.Return()
    return 40
end

return module
0
you should actually explain why, instead of throwing code in peoples faces, dude. Psudar 882 — 4y
0
I did on the post he posted literally 10 mins ago MachoPiggies 526 — 4y

Answer this question