How would I get all the players in my roblox game? I can’t use the Players
service, since it only contains the players that are online on the server, and not the entire game
In what scenario do you need this? You can use the MessagingService service to communicate between servers. MemoryStoreService could also work.
Can I really use MessagingService for that?
And the reason I need this, is because I have a ban system where I want to check if the player is online, but if I use the Players
service, it will just say that the player isn’t online, even though they are but in another server.
You really could just use MessagingService
, or you could use some kind of persistent memory system like DataStore and then when they REJOIN you can kick them from the game, they are banned.
Can I then “loop” through all servers and that way check if a player is in my game that way, or what?
Yes, with the discussed MessagingService.
No, you cannot do that, try using messagingservice.
For every player in game, you could create a topic for them. It doesn’t even have to return anything, just if it exists.
When you need to figure out if a player exists, subscribe to the topic, the function :SubscribeAsync()
will yield until it finds a topic. You could put it into a thread, and after a predefined value, like 3 seconds, if it cannot subscribe, just stop trying. But if it does then you know the player is in game.
If this doesn’t work, then try this, subscribing to a specific topic in every server, maybe named “PlayerBan”. When you get a message from that topic, if the player that is passed is in your server, ban the player!
You don’t need to use MemoryService, MessagingService, Datastores or any of that stuff. Guys, the answer lies right in front of you. Just use APIs. Stop overcomplicating everything.
function ROBLOXMS.GetPlayerCount(): number
local GameJson
local Success, Error = pcall(function()
GameJson = HttpService:GetAsync("https://games.roproxy.com/v1/games?universeIds="..game.GameId)
end)
if not Success then return #Players:GetPlayers() end
local DecodedJson = HttpService:JSONDecode(GameJson)
local Playing = DecodedJson.data[1].playing
if Playing == 0 or Playing == nil then
Playing = #Players:GetPlayers()
end
return Playing
end
I don’t think he’s trying to get HOW many players are in the game, I think he’s trying to get all the players in game so he can find if there is the player in game that he wants to ban.
Weird. Just save it to a datastore. Don’t use anything else.
There are multiple solutions to the same problem, and it isn’t overcomplicating anything. You don’t need to use external APIs to get what you want, some people would prefer not to, as well.
Uh, APIs are far easier, user-friendly, and don’t bloat your game. If he wanted the online players of his game, you would do this 100%. No other way is superior.
The best solution i can think of is this:
Create a MessagingService subscription named “SearchForPlayer” and “FoundPlayer”. Fire the search for player to search every server and wait for FoundPlayer to fire back with the same userid.
If Roblox provides a way of achieving what you want, then I see no issue, in this case though I’d agree, but this isn’t what he’s trying to achieve anyway.
The problem is what you’re giving isn’t even the solution to the problem.
How does your reply contribute to this topic?
“If he wanted the online players of his game, you would do this 100%”
I read the question wrong, and I replied with a helpful solution he could use; datastores.
Try this, subscribing to a specific topic in every server, maybe, named “PlayerBan”. When you get a message from that topic (requesting to ban the player), if the player that is passed is in your server, ban the player! It’s very simple, and easy.
I remember doing this once, so I went ahead and searched for some OLD code of mine, but it still works.
--Some where in a server script
local PLAYERS = game:GetService("Players")
local REPLICATED_STORAGE = game:GetService("ReplicatedStorage")
local SERVER_STORAGE = game:GetService("ServerStorage")
local SERVER_SCRIPTE_SERVICE = game:GetService("ServerScriptService")
local MESSAGE_SERVICE = game:GetService("MessagingService")
local USER_DATA_MODULE = require(SERVER_STORAGE.UserData)
function banSubscribeAsync(userId)
for index, player in pairs(PLAYERS:GetPlayers()) do
if (player.UserId == userId) then
player:Kick("Restricted access.")
end
end
end
--some where in a function used to ban the specified player
local playerById = PLAYERS:GetPlayerByUserId(playerId)
if (playerById) then
playerById:Kick("Restricted access.")
else
MESSAGE_SERVICE:PublishAsync("banUser", playerId)
end
--add player to banned data store/update their data however
NOTE: this does not contain the Data Store portion. If you want to keep them from permanently being able to join again, make sure to use a Data Store for this.