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

how come I am able to get different results from one module script?

Asked by
Trew86 175
5 years ago

Hello, I was expecting this script to not work because according to the developer wiki, "ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require."

However, I was able to change the value of something belonging to my module script.

inside a module script in ReplicatedStorage:

local module = {}

module.smileface = ":)"

module.func = function()
    print(module.smileface)
end

return module

inside a server script in game.Workspace:

local module = require(game.ReplicatedStorage.ModuleScript)

local sadface = ":("

module.func()

module.smileface = sadface

module.func()

The output prints a smiley face then a sad face. I didn't expect this to work, though I am kind of glad it does. How come this is possible even though module scripts supposedly return the exact same value every time they are required?

I'm asking this question because I'm trying to find out if Modules can be manipulated at ease or not.

0
so is it unwise to use modules to store values that are changed in the game? Trew86 175 — 5y

1 answer

Log in to vote
3
Answered by 5 years ago
Edited 5 years ago

This is more to do with how tables work in Lua. The module script is returning a ref to that table you called module and a module script will only ever run once after which a cached value is returned. You can however clone a module script to get two versions.

You then can have multiple scripts using this same table as they all point to this same table. If the table is updated the changes will be shown in other scripts as they read from the same table.

Example

local tbl = {a = 1}


This can be very practical in most cases but in others not so much. You can ceate accessors and mutators functions to get data and functions to set data.

Example:-

-- module script

local curFace  = ":)"
local f = {}

function f.set(str) -- mutator
    -- the idea is that you can validate the str before setting it
    if str == ":)" or str == ":(" then
        curFace  = str
    end
end

function f.get() -- accessor
    return curFace  
end

return f

-- scripts
local module = require(path to module)

print(module.get())

module.set("??")
print(module.get())

module.set(":(")
print(module.get())

Additional info

Primative types (int, string, number) are passed as a copy of in a function call. Functions and tables are pased by ref.

Example

local a = "ab"

local function run(b)
    b = b:sub(2,2) -- b can be safely edited with no modification on the string held in a
    print(b)
end 

run(a)
print(a)

-- using functions 
local function factory()  -- creates a new function which only sets a and returns the prev value
    local a = "ab"

    -- prev value function
    return function(str)
        local tmp = a
        a = str
        return tmp -- pass back prev value
    end
end

local b = factory()  -- holds a ref to a function

local function run2(f, newVal)
    print(f(newVal))
end

run2(b, "b") -- prints ab
run2(b, "c") -- prints b

Hope this helps.

0
Great explanation :) ForeverBrown 356 — 5y
Ad

Answer this question