Variables in a ModuleScript table returning nil?

I have a Module Script that looks like this:

local OtherModules = {
	
	AlivePlayers = {}
	
}

return OtherModules

I want to insert a value into the AlivePlayers table so I do this on a Server Script:

for i,player in pairs(game.Players:GetPlayers()) do
	table.insert(AlivePlayers, player)
end

Now I want to access the values I just put in to the table on a LocalScript so I do this:

for i,v in pairs(AlivePlayers) do
	print(AlivePlayers[1])
	print(v.Name)
end

But it prints nil and nothing after that because the Local Script doesn’t see anything inside of the table, I don’t know why this is happening and I’ve had this issue for about 2 hours now and I’m really confused. If someone could help me that would be great, thanks!

1 Like

Variables are not replicated across server and client. You still need to use RemoteEvents and RemoteFunctions in order to share data between client and server.

So a module script is not a Datastore its just a “class”, it dosent store any Data. You can either Use _G or Remote Events

Server Script

_G.AlivePlayers = {}

for i,player in pairs(game.Players:GetPlayers()) do
	_G.AlivePlayers[#_G.AlivePlayerss+1] = player
end

Local Script

for i,v in pairs(_G.AlivePlayers) do
	print(_G.AlivePlayers[1])
	print(v.Name)
end

Which would be better? RemoteEvents or RemoteFunctions, I’d rather work with RemoteEvents because I’m more familiar with them. As well as if I used a RemoteEvent would I use :FireAllClients to send the information to all of the clients?

Remote Events is better, _G is like meh

Actually, this is incorrect. Modules can still store variables within them, and any scripts which require them can share and access those values (but only within their own network).

@Master_Aaron
You’ll need to create a RemoteFunction which will return a table of Player names or IDs to the client when requested.

I meant like it doesn’t store data like a Data store, you cannot access a data across scripts inside a module script

Could you give me an example of what it’d maybe look like? Again, I rarely use RemoteFunctions so I don’t know how they work, but this would be a great time to start learning them.

For example, you’d have a RemoteFunction (GetAlivePlayers) in ReplicatedStorage and a Script in ServerScriptService which looks something like this:

local OtherModules = require(path.to.OtherModules)
local GetAlivePlayers = game:GetService("ReplicatedStorage").GetAlivePlayers

GetAlivePlayers.OnServerInvoke = function()
    return OtherModules.AlivePlayers
end

Your LocalScript would look like this:

local GetAlivePlayers = game:GetService("ReplicatedStorage").GetAlivePlayers

for i,v in pairs(GetAlivePlayers:InvokeServer()) do
    print(v.Name)
end

Oh ok, I’ll test that when I get home, thanks for the help! :smile:

1 Like

That’s cool. Just be wary when passing Instances with Remotes. If you receive any errors (or nil data), you’ll need to adjust your RemoteFunction to return a table of Player Names or UserIds, then get the Player Instance on the client.