How can i make _G shared across server/clients

Basically I need a way or something like _G that is shared across clients and the server, And I do need it to be a table
And for it to be auto updated like in the example below

-- LocalScript
local X = {"MintIceCream"}
_G.INFO = X
X.INFO[1] = "ChocolateIceCream"
-- ServerScript
local X = _G.INFO
Print(X)
-- Output: ChocolateIceCream
-- (output is just an example and its not actually wokring since its a localscript to serverscript)

It said "ChocolateIceCream" even though in the localscript i set _G.INFO = X before changing the X[1] to "ChocolateIceCream"

I do know if _G.INFO is nil i can just put a event and do shenanigans

The client and the server have different function environment so your example will not work, the best would be to use remote events

1 Like

What I would do is create a module script to be later changed instead of _G

1 Like

For my games I do

local Globals = {}

warn("Loaded!")
--==//Services\\\==--
local rs = game:GetService("ReplicatedStorage")

local players = game:GetService("Players")

local debris = game:GetService("Debris")


local Modules = game:GetService("ReplicatedStorage"):WaitForChild("Modules")

local IsLoaded  = false
--==//Globals\\\==--
function Set()
	local Remotes = rs:WaitForChild("Remotes")

	for _,v in ipairs(Remotes:GetChildren()) do
		_G[v.Name] = Remotes:WaitForChild(v.Name)
	end


	for _,v in ipairs(Modules:GetChildren()) do
	
		if v:GetAttribute("Blacklisted") then continue end
		_G[v.Name] = require(v)
	end
	IsLoaded = true
end


--==//----\\\==--


_G.rs = rs

_G.debris = debris

_G.players = players



Globals["Client"] = function()
	_G.player = players.LocalPlayer
	_G.Camera = game:GetService("Workspace").CurrentCamera

end


function Globals:Wait()
	Set()
	repeat task.wait()

	until IsLoaded
end

--[[



local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Starter_Module = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("Global"))
Starter_Module:Wait()


if character then
	warn('Loaded')
	_G["Skills"]:SetUp()
	_G["Skills"]["Gojo"]()
end


]]
return Globals

Client

local Starter_Module = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("Global"))
Starter_Module:Wait()

Server

local Starter_Module = require(game:GetService("ReplicatedStorage"):WaitForChild("Modules"):WaitForChild("Global"))
Starter_Module:Wait()
1 Like

I dont know how module scripts actually working like, I dont know you can update/store things on there. The thing is I already made the variables and the functions on the localscript

As a note, modules will act the same as _G; Server will see the module as one while the client will see the module as another

Best usage would be to get a RemoteFunction and use a module as a storage between the two

1 Like

Will the example with the minticecream and the chocolateicecream work or not?

As others have said, you’d have to replicate the data using Remote events.

Additionally, I’ve actually made an open-source package for this scenario:

It’s still being documented and tested, but If you’re interested, check it out.

It won’t work in exactly the same way, but you could create a WRITE and READ arguments for the InvokeServer

LocalScript

RemoteFunction:InvokeServer("WRITE",'MintIceCream','ChocolateIceCream')
task.wait(3)
RemoteFunction:InvokeServer("READ",'MintIceCream') -->ChocolateIceCream

Script

RemoteFunction.OnServerInvoke = function(Player, Operation, Key, Value)
if not Storage[Player] then Storage[Player]={} end;

if Operation == 'WRITE' then
Storage[Player][Key] = Value; return true;
elseif Operation == 'READ' then
return Storage[Player][Key] or nil;
end;

end;

I dont think I can really use that since that’s really close to a datastore, And its not going to work, My only idea is a table shared across a server/clients nothing more

I’ll try it out, Thanks for making a module, (I’m not used to using modules)

That’s exactly what it does.

You won’t be able to share variables between the client and the server otherwise.

I do have an idea, I dont know how to do it though. Its that the server has a table and the client has a different one, so when the client table changes/updates it overwrites the server table and the other way around.

As I have mentioned, you can’t replicate client changes to the server without a remote event or a remote function.

What I have sent is the most basic way to share a table between the server-client while also keeping other clients secure.

Oh you did that to make it secure for other clients, I were making the server size 1 and its not for that reason only, The only reason I have it on a script is because I needed to use Loadstring

Without external remotes/modules, No you cant. (atleast that’s what i think so)

Ill use remotes, That’s fine I just need to think of a way to do it.