Shared variables between local scripts on a single client

I want to share a variable between two local scripts on the same client without merging the code into one local script. I haven’t found a good way to do this and I would prefer NOT using remote events.
The idea is that one local script monitors inputs using the UserInputService and updates a table so that the other local script can read from it each frame to make decisions based on the input table.

So far I’ve read similar posts stating using _G and module scripts, but it was not explained in detail how that works. I’m familiar with module scripts, but from memory and experimentation, I can’t use a local script to manipulate a variable in a module script and expect a different local script that required it to have the same manipulated variable.

What can I do?

1 Like

May I ask why you’d prefer to not use Remote Events?

Also, what variable is this? If it’s something like a boolean value, or integer, then you can simply place one in Replicated Storage.

Well, Remote events are usually slow due to the physical space the data must travel and I’m concerned that using them for client-to-same-client communication might require travel outside my client (for reasons unknown) thus slowing down the update.

This variable is a nested table. The top level keys are strings, like a dictionary. The 2nd level is a list of strings.

Yes you can, if one script changes a value in a module script, another script, if it’s the same class of script, can access the changed value:

local Value = 1
local module = {}
function module.Increase()
    Value = Value + 1
end
function module.Get()
    return Value
end
return module
--// Script 1
local Mod = require(ModulePath)
Mod.Increase()
--// Script 2
local Mod = require(ModulePath)
print(Mod.Get()) -- 2

To make this work the first script has to change the modules value before the other script gets it.

1 Like

Ah I see, however I need to continuously update the variables every frame. Your suggestion only works for a one-time change, correct?

Why not use a bindable event?

I think your solution works, however I’m having some silly bugs with my code, so it’ll take me a while to verify.

Thanks for your help!

Haven’t used them before, but might look into that. Thanks.