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!
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.
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?
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.
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
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.