How to get a Players total visits

Hello Fellow Developers, I wanted to ask how I would go about making a system that counts a player total visits. I have looked through the API documentation from Roblox and there is no API to get the players visits. I have also looked through a few dev forum posts about this and I can’t find the solution. I hope you guys can help me figure out how to do this.

Thanks,
ViridianDevil

1 Like

Just use the :GetPlayers method on Players Service, which returns a table of all player instances and then the length of that table using the # operator is your number of players in the server.

What about saving their visits with a DataStore?

1 Like

That’s not what I’m trying to do. I’m trying to retrieve the place visits from the players profile. like total game visits.

1 Like

I don’t know much of this but you can try setting up a Proxy to Roblox’s API servers to access their API and get their place visits via HTML formatting. Search up how to set-up proxy’s

I’m aware on how to set up proxies but Roblox does not have any APIs that directly let you retrieve a players visits.

1 Like

You can get all games from an user and calculate their visits from there like so:

local httpService = game:GetService("HttpService")

local function fetchTotalVisits(playerId)
	local requestLink = "https://games.roproxy.com/v2/users/" ..playerId.."/games"
	
	local response = httpService:GetAsync(requestLink)
	
	local totalPlaceVisits = 0
	local tableResponse = httpService:JSONDecode(response)
	
	for _, gameInfo in pairs(tableResponse.data) do
		totalPlaceVisits += gameInfo.placeVisits
	end
	
	return totalPlaceVisits
end


print(fetchTotalVisits(85103695))

Private games may not accounted for, but i think this is your best bet, atleast until roblox releases a way to get a player’s total visits from their profile.


Please refer to my updated post:

1 Like

Is there an alternative to this that would also account private games? I heard web scraping could be helpful for this but I have no clue on how to do it.

Due to privacy reasons i don’t think you can for now, of course you can look at Roblox’s APIs yourself but i did not find a better way unfortunately.

Flex Your Account Age is doing meaning it’s possible.

1 Like

Figured it out, i was wrong, the games api does actually get all games it’s just that by default the limit is 10

Old:

local requestLink = "https://games.roproxy.com/v2/users/" ..playerId.."/games

Fixed:

local requestLink = "https://games.roproxy.com/v2/users/" ..playerId.."/games?sortOrder=Asc&limit=50" --Sets the limit to the max possible which is 50

Now it prints the correct amount of visits :slight_smile:


Final Code:

local httpService = game:GetService("HttpService")

local function fetchTotalVisits(playerId)
	local requestLink = "https://games.roproxy.com/v2/users/" ..playerId.."/games?sortOrder=Asc&limit=50"
	
	local response = httpService:GetAsync(requestLink)
	
	local totalPlaceVisits = 0
	local tableResponse = httpService:JSONDecode(response)
	
	for _, gameInfo in pairs(tableResponse.data) do
		totalPlaceVisits += gameInfo.placeVisits
	end
	
	return totalPlaceVisits
end


print(fetchTotalVisits(85103695)) --> Prints user's total visits
1 Like

What if you have more than 50 games?

Is there a way I could web scrape?

I’m sorry but i do not know what is or how to web scrape, maybe even @Rdite could help you further or say how he did it but i’m fairly certain this is how he checks.

1 Like

no clue about web scraping personally but you can send multiple requests using “nextPageCursor” to get more than 50 games.

...... /games?accessFilter=Both&sortOrder=Asc&limit=50&cursor=".. page

2 Likes

Thanks for help I would mark both you and Rdite as a solution if I combine what you wrote with what Rdite said I could make this work!

1 Like

@mniao I’m unable to figure out how I could apply this. This is what I have currently:

1 Like

Sorry for my really late reply, but i made the code to properly get the total place visits and made it more accurate as previously only starter places were accounted for:

local httpService = game:GetService("HttpService")

local gamesApiCall = "https://games.roproxy.com/v2/users/%s/games?sortOrder=Asc&limit=50" --the api link, you can change the configurations or link.
local universeApiCall = "https://games.roproxy.com/v1/games?universeIds=%s" --universe info api link, this is used to get the game's total visits as previously only starter places were calculated

local function httpGetAsync(...) --let's make a function for this so we don't have to pcall every time
	local response = nil
	local args = ...

	local success, msg = pcall(function()
		response = httpService:GetAsync(args)
	end)
	
	if success and response then
		return response
	else
		warn(msg)
	end
end

local function fetchTotalPlaceVisits(playerId)
	local totalPlaceVisits = 0
	
	local requestLink = string.format(gamesApiCall, playerId)
	local newRequestLink = nil
	
	while true do	
		local response = httpGetAsync(newRequestLink or requestLink, false)
		
		if response then
			local tableResponse = httpService:JSONDecode(response)
			
			for _, gameInfo in pairs(tableResponse.data) do
				local universeResponse = httpGetAsync(string.format(universeApiCall, gameInfo.id), false)
				
				if universeResponse then
					local universeTableResponse = httpService:JSONDecode(universeResponse)
					totalPlaceVisits += universeTableResponse.data[1].visits
				else
					warn("Unable to retrieve universe information, using place visits instead")
					totalPlaceVisits += gameInfo.placeVisits
				end
			end
			
			if tableResponse.nextPageCursor then
				newRequestLink = requestLink .. "&cursor=" .. tableResponse.nextPageCursor
			else
				break
			end
		else
			break
		end
	end
	
	return totalPlaceVisits
end

print(fetchTotalPlaceVisits(85103695)) --YIELDS FOR ABOUT 1-10 SECONDS DEPENDING ON HOW MANY GAMES THE USER HAS.

This is not a very efficient way as multiple API calls are made but it is certainly accurate, this is the only way to do this as roblox does not give us a way to read the total visits, if roblox ever releases an API or any method in the future that allows you to read total place visits directly instead of having to calculate it like this i highly recommend you swap to that, but as for now you can use this.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.