Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I have a TeleportService Question?

Check how many players are within a universe's game? How do you get the players info that are in the universe's game (their name)?

Games like Apocalypse Rising, can manage this without the TeleportService having no functions to do such things. Unless I'm reading them wrong..

I don't know where to start with something like this.

1 answer

✓ 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:

-- this script, in theory, should create a DataStore for how many servers are up and running in a universe

local ds = game:GetService("DataStoreService"):GetDataStore("ServersRunning")
ds:UpdateAsync("numberOfServers",ds:GetAsync("numberOfServers")+1 or 1) -- adds the current server to the datastore

game.Players.PlayerRemoving:connect(function(p) -- when a player leaves
	if game.Players.NumPlayers == 0 then -- may not work, if not, change to 1
		ds:UpdateAsync("numberOfServers",ds:GetAsync("numberOfServers")-1 or 0) -- updates the datastore
	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

0

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?

0

Yes, it can. It is has nothing to do with individual servers.

This discussion is preserved as it was. New answers, comments, and votes are closed.