How to work with player states between scripts

I doing a big game and now I have scripts like CameraBobbing, Footsteps, WeaponController and I need to store states or other data somewhere in table, and share with other scripts. I should have shared table with data of player on local scripts, and shared table with data of all players in server scripts. But how I can organize that, and update of all states like Aiming, Sprinting and others?

Modules are your friend for this!

You should use modulescripts. Modulescripts can be “required” by any kind of script: server scripts, local scripts, and even other modulescripts! When it is required, it returns any kind of value, including tables and even functions. This can be used to make complex systems easier to use, because the thing it returns is shared across all the scripts that required it. For example, here is our imaginary client-side modulescript:

local myTable = {}

myTable.running = false
myTable.Crouching = false
myTable.cam = workspace.CurrentCamera

return myTable

And here are two different client side scripts:

local requiredTable = require(ourModuleScript)

wait(10)
requiredTable.running = true
local requiredTable = require(ourModuleScript)
print(requiredTable.running)
wait(20)
print(requiredTable.running)

At first, the second local script will print “false”, but then since the seperate script changed the “running” value to “true”, after the 20 seconds is over the second script will print “true”. This makes modulescripts very useful for large systems that need to be synced.

im already worked a lot with them but mostly I used them with metatables, like my FPS module, and Custom health system module. So right now I know so changing value of module in some other script will change it for everyone (local script changes will be visible only for other local scripts). But if I will change some value in client version of module and change same value in server version of module does change’s will replicate and update to client version, or value will be same as client set?

I’ve tested it when the client changes it and if the server changes it to see if it was replicated- it wasn’t. So, when changing the values in the table it only changes locally and does not replicate. I hope this answered your question!

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