Hi!
What is the best way to request a value and send it from one server script to another?
For example, if I had two scripts in ServerScriptService, one called DataScript and the other called ReceiveScript, what would be the best way to request a variable from DataScript (in my case a dictionary) and send it to ReceiveScript for further use of it?
Also, what would be the best way to send some data to the DataScript to overwrite data in it?
I know remotes and bindables exist, but I was wondering if there was an even better way of sending data.
Maybe a modulescript? If you have your Data in a modulescript you can create a function in your module that returns the data.
Edit: So a very VERY simplified version could look a little something like this -
Data Modulescript-
local dataModule = {}
local playerData = {}
function dataModule.getPlayerData(player)
return playerData[player]
end
return dataModule
ServerScriptServer Script -
local dataModule=require( game:GetService("ReplicatedStorage"):WaitForChild("DataModule"))
game.Players.PlayerAdded:Connect(function(player)
local plrData = dataModule.getPlayerData(player)
end)
Depending on how you use your data you can put the ModuleScript in ReplicatedStorage. If you expect the Player to retrieve the data and show it in their UI or somewhere else you can leave it in RS but if you donāt need the Player touching their data you can put the ModuleScript under ServerScriptService or wherever you want.
Are you adverse to using bindables in your structure or are you looking for further simplification? I donāt really think there is a ābetter wayā here.
Bindables help for communication between scripts, but one of the bottlenecks is that they pass by value and also make a copy of the passed data. This is typically why developers set up custom signal classes, so they can pass by reference with unedited input.
You donāt need bindables or any custom pseudoclasses either. You can communicate directly between scripts and just return data. For example, you can have one of the scripts be a ModuleScript that exposes methods for querying data, while the other runs all itās own code via a regular script and doubles up as a bootstrapper for the ModuleScript. You could just run, for example, DataModule:Get(key) or whatever.
Thereās also the method of _G but I wouldnāt if youāre not as experienced a programmer. You can get lost and fall into traps with it. Iāll leave that topic alone.
This is a really nice solution! Being able to easily access the data from any script that can access the modulescript sounds like just what I needed! Iāll look more into that.
Well, I am looking for the best and most reliable way, not the easiest way :)
Modules are very reliable, Iāll give you an example:
local module = {}
local dataTable = {}
function module.get(key)
return dataTable[key] or dataTable
end
function module.set(key, value)
dataTable[key] = value
end
return module
Tables are separated into Keys, so you can get and set existing values and set non-existing values.
Basically, you can use the get function to get values from the table and you can get the entire table by not supplying an argument to it.
The set function can set pre-existing values and create new values. You use the set function to set existing Keys not accessible by the function. You can do that like this:
local subValue = module.get(player.Name)
subValue.coins = 0 -- a value inside the table gotten from the get function
module.set(player.Name, subValue)