What is the best way to request a value and send it from one server script to another?

Hi! :smile:
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.

Thanks in advance,
Mun :Š—

1 Like

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.

2 Likes

BindableFunctions, they work like their remote counterpart, but they are for two server scripts. It’s a pretty simple solution, but it works well.

For example:

-- Server 1
BindableFunction.OnInvoke = function(arg1) -- data and stuff
	return dataTable[key]
end

-- Server 2
BindableFunction:Invoke("cash")

Edit: I just realized you said you know that bindables exist: The solution above works, but the easiest to set up is bindables.

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.

1 Like

Thanks for the replies :D

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)

Good luck on making your game!

Thanks :slight_smile: This is a nice explaination :D

1 Like