How to make variable change for all scripts

I want variables in module scripts to change in all scripts. How would i do this? For example if i had a variable as 100 and i had a script that reduces it by 20, i want it when another script uses it, it will be 80. How would i do that?

5 Likes

If you store the variable inside of a regular table and modify it, it will change for all scripts of the same replication (as in, if the value is modified in a server script it will change for all server scripts that will read the value after it was changed)

1 Like

i stored it like this:

local module = {}

module.variableName = 100;
return module
1 Like

In that case you should be able to change it in one server Script then read the new value from a different server Script

1 Like

I have a serverside script (i think white colored ones are serverside) which decreases the variable, however when i tried to print it in another script it printed the same value and didnt decrease. But when i printed it in the script which made the variable decrease, the printing did decrease. fo you need the scripts to see what’s wrong?

2 Likes

You need to wait for some time to pass by using task.wait before reading the new value, otherwise it will print the original value

1 Like

here are the scripts:

script 1:

local module = require(game.ReplicatedStorage.variables.module)
local variable = module.variable

repeat
	wait(1)
	variable = variable - 1
until variable < 0

script 2:

local module = require(game.ReplicatedStorage.variables.module)
local variable = module.variable

while true do
	wait(0.1)
	print(variable)
end
2 Likes

You’re only reading the variable once when you do

local variable = module.variable

You need to do this instead:

local module = require(game.ReplicatedStorage.variables.module)

while true do
	wait(0.1)
	print(module.variable)
end

Edit: @youandm389 You’ll also need to modify the script where you’re changing it too:

local module = require(game.ReplicatedStorage.variables.module)

repeat
	wait(1)
	module.variable -= 1
until module.variable < 0
2 Likes

it still prints 100 even though the variable is decreasing. (there was also a print script in script 1 and the variable went down.)

1 Like

You need to update the script that’s changing the value too, you can’t store a property in a variable

1 Like

how would i update the script?

Like in the edit I made for the original reply:

1 Like

i tried that, it didnt change it.

2 Likes

You need to do it like in this place file: ModuleExample.rbxl (53.2 KB)

2 Likes

i want to ask something: what’s the difference between task.wait and wait()?

2 Likes

task.wait talks directly to Roblox’s task scheduler which makes it more efficient to use, and currently runs at 60fps while the original wait function always runs at 30fps no matter how fast the device is

2 Likes

alright. by the way, does it being a local script affect it? cuz the console look slike this:
20:43:39.450 100 - Client - LocalScript:9
20:43:39.515 85 - Server - Script:13
20:43:39.550 100 - Client - LocalScript:9
20:43:39.615 84 - Server - Script:13
both refer to the same variable, however the problem is apparent

1 Like

When requiring a module from a LocalScript, the LocalScript recieves a default version of the module even if you previously change a value using a server Script. You can test this by printing the memory address of the table that’s returned by the module

3 Likes

so i should use a server side script then?

1 Like

It depends on where and how you’d like to use your module, if you’re using it on the server then yes but if the module is only used by the client then no

Luckily changing a value of the module from a LocalScript will also make the value change for other LocalScripts that require the module (but not for server Scripts, as in changing the value on the client doesn’t change it for the server)

1 Like