How to get individual place data within a game?

I am currently trying to create a game menu that lists individual buttons that tells the player how many players are within a place, which the player can teleport to at will.

The problem that I’m having is getting each individual place’s player count inside of my game. I have tried using Datastore to update when new players join, but I found that to be extremely unreliable and slow to update incoming players. I also tried using ROBLOX’s API endpoints, but then I was only able to get the amount of players in my game as a whole and not each individual place, which isn’t what I wanted. Does anyone know what I could be missing, or any solutions I could use to get my desired result? Any feedback at all would be appreciated.

you could try use this

I have seen that on many forum posts before posting this, but I couldn’t figure out how to use it to know how many players are within each place in my game, how would I be able to do this?

I would honestly think of this a bit like an invoke of a RemoteFunction without a definite end.

I would create a unique identifier using HttpService’s GenerateGUID and then use MessagingService’s SubscribeAsync to create a topic of the GUID. PublishAsync with a topic of “GetPlayers” and the message of the identifier.

In all of the other games, I would use SubscribeAsync with a topic of “GetPlayers”. Every time this gets triggered, I would then use PublishAsync with the topic of the unique identifier and pass along how many players were inside that server and the placeId.

I would then grab all of that data from the first SubscribeAsync and plop it inside of a table, wait a few seconds and disconnect the original subscription- and there would be your results. I highly suggest to cache the data for around two minutes before running this again because it might go over the MessagingService limit.

Script that receives data

local uniqueTopic = httpService:GenerateGUID( false )

local playerCounts = { -- Add more to support your game.
	[PLACEID] = 0,
}

-- You should pcall this.
local connexion = messagingService:SubscribeAsync( uniqueTopic, function( message )
	local split = string.split( message.Data, "_" )
	local placeId, number = tonumber( split[1] ), tonumber( split[2] )
	
	playerCounts[ placeId ] += number
end)

messagingService:PublishAsync( "GetPlayers", uniqueTopic )

wait(2)

connexion:Disconnect()
print( playerCounts )

Script that gives data

-- You should pcall this.
messagingService:SubscribeAsync( "GetPlayers", function( topic )
	messagingService:PublishAsync( topic.Data, tostring( game.PlaceId ) .."_".. tostring( #players:GetPlayers() ) )
end)

I obviously didn’t include the variables. Feel free to ask any questions. If any others have a better way of doing this I’d love to know since I don’t think this way of communicating data is efficient.

1 Like