Hi, I would like to know how to express a player’s account age in years. I also looked on the developer hub but found nothing about it. I appreciate any help.
divide it by 365
3 Likes
then math.round that val so its not like 50 digits
1 Like
What form is the data you currently have?
If you’re getting the account age via ROBLOX’s web API, simply:
- years = currentYear - yearUserJoined
localplayer.AccountAge
(character limit)
game:GetService('Players').PlayerAdded:Connect(function(playerJoined)
local years = math.floor(playerJoined.AccountAge/365)
end)
3 Likes
I know you’ve since deleted your comment, but I’m posting this to provide a resource to anybody who might need it whilst stumbling across OP’s post.
In a use case where AccountAge
is 0 years, but a value not equal to 0 is required, you could implement something similar to the following to return the first significant figure of account age:
-- Assuming that the days -> year conversion returns this
local num = tostring(0.01234)
-- Iterates the length of the string of account age in years
for i = 1, #num do
-- Checks the current substring of position i and determines whether
-- it is a value not equal to 0
if num:sub(i, i) ~= "0" and num:sub(i, i) ~= "." then
-- If a number not equal to 0 is found, formats the string to
-- round to the value of (i - 2)
-- Compensates for the first two characters [0 and .] respectively
num = string.format("%." .. tostring(i - 2) .. "f", num)
-- Terminates the loop
break
end
end
print(num)
that’s a pretty good implementation, better than my own attempt
1 Like