Is it possible to check when did a player acquired a badge from my game?

And how about game passes?

You can store this kind of Data through a DataStore. You can store when a badge is obtained by a player, but you would not be able to store the time if a player bought it from the website. This link shall explain to you how it works. Go to the Giving the GamePass an effect and at the second example where it says “Here’s an example for giving effects directly after purchase” and replace the printing it has with DataStore stuff or if you want it to store in another website like trello simply use a TrelloAPI ModuleScript. For badges, simply use DataStore or Trello again and add the code that saves the time etc. after the badge is awarded. You can also use the Google Spreadsheets to store this kind of stuff.

5 Likes

You could technically use a node.js solution (Roblox-js exists but that’s not being updated anymore) to stay logged into your account and log timestamps for when players buy a pass from the site.

For gamepasses & developer products you’d need to utilize the facilities provided by Roblox’s DataStoreService as currently there are no API endpoints that allow you to query when a particular user purchased them.

Badges on the other hand do have an API endpoint that supports this kind of query.
https://badges.roblox.com/docs#!/BadgeAwards/get_v1_users_userId_badges_awarded_dates

Here’s a script I wrote which retrieves the times & dates at which a set of badges were earned by a particular user. Comments have been attached in order to provide additional details/information regarding the code snippet.

local players = game:GetService("Players")
local http = game:GetService("HttpService")
local get = http.GetAsync
local jsonDecode = http.JSONDecode
local badges = game:GetService("BadgeService")
local userHasBadge = badges.UserHasBadgeAsync

local proxyUrl = "roproxy.com" --Change this to your proxy's domain.
local baseUrl = "https://badges."..proxyUrl.."/v1/users/%d/badges/awarded-dates?badgeIds=%s"
local badgeIds = {} --Array of badge IDs (maximum length is 100).

local function onPlayerAdded(player)
	local earnedBadges = {} --Array to store earned badges.
	for _, badgeId in ipairs(badgeIds) do --Iterate over badge IDs.
		task.wait(0.1) --Will suspend for a maximum of 10 seconds (100 * 0.1).
		local success, result = pcall(userHasBadge, badges, player.UserId, badgeId) --Check if user owns the badge.
		if success then
			if result then
				table.insert(earnedBadges, badgeId)
			end
		else
			warn(result)
		end
	end
	
	if #earnedBadges > 0 then --Make sure the user has at least one of the checked badges.
		local requestUrl = string.format(baseUrl, player.UserId, table.concat(earnedBadges)) --Issue HTTP request.
		local success, result = pcall(get, http, requestUrl)
		if success then
			if result then
				local success2, result2 = pcall(jsonDecode, http, result) --Decode the HTTP response's body which is in a JSON format.
				if success2 then
					if result2 then
						for _, badgeItem in ipairs(result2.data) do --Iterate over the queried badges.
							print(badgeItem.awardedDate) --Print the badge's awarded date.
						end
					end
				else
					warn(result2)
				end
			end
		else
			warn(result)
		end
	end
end

players.PlayerAdded:Connect(onPlayerAdded)
2 Likes