✓ Accepted answer
Great question, it seems to be really popular right now (this is the third one is 2 (?) days).
The simple answer to this is DataStores.
There are no functions or anything official you can use to get the players in a universe. They should probably do something like this. Luckily, DataStore limitations are way higher than the requests we need, so let's get straight into it:
DataStores
DataStores are useful way to send and recieve data between servers. They allow you to store data on the ROBLOX servers, so you know, for example, how many kills a player has in total, out of all of their play times.
It has 4 main functions:
- :GetDataStore(name)
- :GetAsync(key)
- :SetAsync(key,value)
- :UpdateAsync(key,value) (This is better for when multiple servers are saving data at once)
Here's how to use it:
local ds = game:GetService("DataStoreService"):GetDataStore("ServersRunning")
ds:UpdateAsync("numberOfServers",ds:GetAsync("numberOfServers")+1 or 1)
game.Players.PlayerRemoving:connect(function(p)
if game.Players.NumPlayers == 0 then
ds:UpdateAsync("numberOfServers",ds:GetAsync("numberOfServers")-1 or 0)
end
end)
while wait(5) do
print("Number of servers running = " ..ds:GetAsync("numberOfServers) or 0) -- prints how many servers are running every 5 seconds
end
Hopefully this example helped.
Making the player list
So, the recommended way to do this is by using a table. It may be slightly buggy when players are doing things at exactly the same time, but it shouldn't happen much (if at all).
local ds = game:GetService("DataStoreService"):GetDataStore("Players")
game.Players.PlayerAdded:connect(function(p)
local tab = ds:GetAsync("list")
tab[p.Name] =p.UserId
ds:UpdateAsync("list",tab)
end)
game.Players.PlayerRemoving:connect(function(p)
local tab = ds:GetAsync("list")
tab[p.Name] = nil
ds:UpdateAsync("list",tab)
end)
Hope I helped! I don't have time to explain, but if you need any more help, I'll be there :P
~TDP
2 comments
Okay this actually makes a good amount of sense. Another question would be, can the datastore be called in all the games within a universe?
Yes, it can. It is has nothing to do with individual servers.