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

[solved] How to share ModuleScript across Services?

Is it possible to edit variables in a Module and have those edits shared across Services?

I've created the following:

StarterPlayer/StarterPlayerScripts/test (ModuleScript)

local module = {}

module.x = 10

function module:setX(value)
	self.x = value
end

return module

StarterPlayer/StarterPlayerScripts/test2 (LocalScript)

local test = require(script.Parent.test)

test:setX(5)

print(test.x)
  • outputs 5

StarterPlayer/StarterPlayerScripts/test3 (LocalScript)

local test = require(script.Parent.test)

wait(5)
print(test.x)
  • outputs 5

StarterPack/Kickball/tool (LocalScript)

local test = require(game.StarterPlayer:WaitForChild("StarterPlayerScripts"):WaitForChild("test"))
wait(6)
print(test.x)
  • outputs 10

I would have expected the last script (tool) to output 5 since the other script changed the value but this is not the case, it's like the module was copied fresh

1 comment

0

Had a little discussion with some other guys as to why this is but it's because Playerscripts are cloned to the Player.PlayerScripts, which makes script.Parent and game.StarterPlayer two separate instances and thus 2 different contexts (thanks to hiimgoodpack)

2 answers

When you require a module you are obtaining the same context every time. Do note that the server and client both use a different context though.

If you were to call a function that edits a local variable in the module, and then retrieve that value in the other module, it will have been updated.

If you edit it externally (into the required table) I'm not sure if that works. I'd have to verify that.

1 comment

0

Thanks for the response, I just updated my example to reflect your 2nd paragraph (if I interpreted you right)

Moving the ModuleScript to ReplicatedStorage solved this issue.. all LocalScripts now output 5

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