hey i am trying to do simple code that will show in the leaderboard join year of the player, i got stuck with it
game.Players.PlayerAdded:Connect(function(player)
local joinYear = os.date("%Y", player.AccountAge)
print(player.Name .. " joined in " .. joinYear)
end)
Its printing the account age, not the date of join. That number is how days that user has been on the platform. So getting the current day - AccountAge, which give you the day they joined. So try using some math to figure out what year it is.
2649 is the days, first you need to divide it by 365, which in return will be 7.25…, then use math.round and then you subtract the current year with the number (7 in this case). And the number you get, in this example, 2023 - 7 is 2016.
game.Players.PlayerAdded:Connect(function(player)
local date = os.date("*t")
print(date) -- prints the table, you can check what things you can get
local year = date["year"] -- the year
print(year)
local playerDays = player.AccountAge
local subtractNumber = playerDays / 365
local roundSubtractNumber = math.round(subtractNumber)
local playerAge = year - roundSubtractNumber
print(player.Name .. " joined in ".. playerAge)
end)
you should probably familiarise yourself with how timestamps work
if you want 100% accuracy then you should use the apis and not divide the account age by 365 because that could have false flags if a leap year is involved (now that i think about it, it’s very unlikely to cause an issue but it’s still a probability and i prefer accuracy )
but since roblox is TOO PARANOID and doesn’t let you use their apis in-game (although they should just use unauthenticated requests but whatever), you’d have to use a proxy such as https://users.roproxy.com/v1/users/55495469 and extract the ‘created’ field
"created":"2014-02-03T19:16:57.357Z"
you can simply accomplish this by doing this
local HttpService = game:GetService("HttpService")
game.Players.PlayerAdded:Connect(function(player)
local success, response = pcall(function()
return HttpService:JSONDecode(HttpService:GetAsync("https://users.roproxy.com/v1/users/" .. player.UserId))
end)
if success and response ~= nil then
local joinYear = string.split(response.created, "-")[1]
print(player.Name .. " joined in " .. joinYear)
else
print("something went wrong. try enabling HTTP requests")
end
end)