Issue with sharing a value between two lcoal scripts using a single module script

StaminaManager is a module script.

This is the contents of StaminaManager:

StaminaManager is required in LocalScriptA:

StaminaManager is also required in OtherModule, which is then required in LocalScriptB:

Summary of my problem

The issue I’m having is that when StaminaManager.setTestValue(newValue) is called from one local script, the other local script is unable to see the newly set value using StaminaManager.getTestValue().

Furthermore, when I set the testValue from LocalScriptA, the event connection for testValueChanged within LocalScriptA only receives the fired signal. It’s the same when I set the testValue from LocalScriptB, the testValueChanged event connection within LocalScriptB only receives the fired signal.

What I'm trying to achieve

I would like for the changes to testValue from one local script be visible to the other local script. From my current understanding of module scripts, since this module script is being required from two local scripts which have the same run environment (the client), I would expect this to be the deafult behavior, but now I’m just confused.

I think that the script that receives the shared value got executed before the script that sets the shared value

try adding a task.wait(number) at the start of the script that receives the shared value and if it printed the correct value then this is the case

1 Like

Ok, so I plugged my dev forum post into chatgpt and it pointed out that the likely issue is that LocalScriptA and LocalScriptB aren’t actually requiring the same module script and that I should check the paths to make sure each LocalScript is requiring the same path.

I thought at first, how could this be? since I don’t ever recall cloning StaminaManager during runtime, but then I realized that StaminaManager was in StarterCharacter scripts, and so was LocalScriptA, but not LocalScriptB.

So ultimately, the problem was that LocalScriptA was requiring a replicated version of StaminaManager while LocalScriptB was requiring the original version of StaminaManager, which why they weren’t sharing their values

1 Like

That came to mind for me too, but I made sure that the testValueChanged connection was connected before using setTestValue(). Thanks for your input though

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.