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

how do i get the total players currently playing my game?

i want to print out the ammount of players in my game but i dont know how to do that, i found this script but i dont know how to use it:

local body = game:GetService("HttpService"):JSONDecode(res) --res would be the response from the server

local playerCount = body.data[1].playing
print(playerCount)

i also get an error Argument 1 missing or nil in Line 1

1 answer

The script you provide above is just a sample. To make it work, you need some edit to the script:

local HttpService = game:GetService("HttpService") -- we need the Http service to do this

local URL_Game = "https://games.roblox.com/v1/games?universeIds=0000000000" -- your game's id

local function GetGameData()
	local Response
	local GameData

	pcall(function()
		Response = HttpService:GetAsync(URL_Game) -- this will return JSON string that store your game data
		GameData = HttpService:JSONDecode(Response) -- de-code the JSON string to Lua table
	end)
	
	if not GameData then return -- check is the request is success or not
	else return GameData end-- if success then return GameData
end

local CurrentPlayers = GetGameData().data.playing -- get the current player of your game

print(CurrentPlayers) -- print it out :D

*Note: Maybe this not work because I'm new to Http stuff myself

Hope this helped you :D

8 comments

0

get an error at line 18 saying "attempt to index nil with 'data'"

0

Try GetGameData().playing instead , im on mobile right now so i cant test it.

0

Or maybe we fail to get the data i guess :/

0

nope same thing just the data is replaced with playing "attempt to index nil with 'playing'"

0

Ok so I check the URl and nothing there, maybe it is Roblox side issue. You can check the sample URL here and you can see, there is nothing: https://games.roblox.com/v1/games?universeIds=4789013654

0

ok so do i just wait for roblox then?

0

Yeah, just waiting :| . It Roblox issue, I dunno how to fix it :V

0

oof, ok thx anyways ;-;

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