I’m trying to find the account age of players that are not in game but “data” always returns nil. Why is that? Is there any way to fix it? If not then how can I find the account age of players that are not in game?
local HttpService = game:GetService("HttpService")
local URL = string.format('https://users.roblox.com/v1/users/%s', targetUserId)
local response
local data
pcall(function()
response = HttpService:GetAsync(URL)
data = HttpService:JSONDecode(response)
end)
local createdTime = DateTime.fromIsoDate(data.created).UnixTimestamp
local currentTime = DateTime.now().UnixTimestamp
local accountAgeDays = (currentTime - createdTime)/86400
script.Parent.TargetAge.Text = accountAgeDays
The reason data is nil is because the body of your pcall is erroring. You should see what the error result is from the pcall because that will help you identify what the problem is.
local success, errMessage = pcall(function()
response = HttpService:GetAsync(URL)
data = HttpService:JSONDecode(response)
end)
if not success then -- If the call failed we can output the error message
warn("User API request failed:", errMessage)
end
Also here is a fancier trick to get the stack trace of the error with it (which tells you exactly where the error happened). It works the exact same way as pcall but you can add an error handler function after the function you pass which will change what error message is returned. (In this case, Roblox calls debug.traceback with the original error message so it basically sticks the traceback after the error message like you would see in the output normally)
local success, errMessage = xpcall(function()
response = HttpService:GetAsync(URL)
data = HttpService:JSONDecode(response)
end, debug.traceback)
I found the issue. I used a RemoteFunction to provide the UserId to a Sever Script (I was trying to get a response from a Local Script) then I returned the account age from the Server Script to the Local Script.