Advertisement
Guest User

Roblox Unit Conversion API

a guest
Sep 6th, 2019
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. local API = {}
  2.  
  3. --[[ If the 'Canon' argument is true, measurements will be returned according to an official 2012 blogpost.
  4. Keep in mind this blogpost states that a robloxian is 25 cm tall, roughly 1/5th an earthlike scale... ]]--
  5.  
  6. --[[ If the 'Canon' argument is false OR nil, measurements will be returned as experienced estimations.
  7. They will also be closer to true earthlike scale ]]--
  8. API.RealMultipliers = {
  9.     F = 0.97012098544007168083155285088634,
  10.     I = 11.641451825280860169978634210636,
  11.     Y = 2.910362956320215042494658552659,
  12.     MI = 5122.2388031235784747905990526799,
  13.     MM = 0.0031828117338712047733393918553019,
  14.     CM = 0.031828117338712047733393918553019,
  15.     M = 3.1828117338712047733393918553019,
  16.     KM = 8243.4237954188997826195026794399,
  17. }
  18.  
  19. API.CanonMultipliers = {
  20.     F = 7.2907334,
  21.     I = 87.4888,
  22.     Y = 21.8722,
  23.     MI = 38495.072352,
  24.     MM = 2000,
  25.     CM = 200,
  26.     M = 20,
  27.     KM = 2,
  28. }
  29.  
  30. API.TranslationTable = {
  31.     FT = "F",
  32.     FEET = "F",
  33.     IN = "I",
  34.     INCHES = "I",
  35.     YARDS = "Y",
  36.     YD = "Y",
  37.     YRD = "Y",
  38.     MILES = "MI",
  39.     MILIMETERS = "MM",
  40.     CENTIMETERS = "CM",
  41.     METERS = "M",
  42.     MT = "M",
  43.     MTR = "M",
  44.     KILOMETERS = "KM",
  45.     KMT = "KM"
  46. }
  47.  
  48. local Ref = "Real"
  49.  
  50. local function DeferToWhat(Canon) if Canon ~= nil and Canon == true then Ref = "Canon" else Ref = "Real" end end
  51. local function ParseUnit(Unit) Unit = string.upper(Unit) if API.TranslationTable[Unit] ~= nil then Unit = API.TranslationTable[Unit] elseif API.TranslationTable[Unit.."S"] ~= nil then Unit = API.TranslationTable[Unit.."S"] end return Unit end
  52.  
  53. function API.Help(Req)
  54.     print("Roblox Unit Conversion v1.0 Help: ")
  55.     if Req == nil then Req = "General" end
  56.     Req = string.lower(Req)
  57.     if Req == "unit" then
  58.         print("You can only convert to and from the following units of measurement: ")
  59.         print("feet, f")
  60.         print("inches, i")
  61.         print("yards, y")
  62.         print("miles, mi")
  63.         print("milimeters, mm")
  64.         print("centimeters, cm")
  65.         print("meters, m")
  66.         print("kilometers, km")
  67.     elseif Req == "api" then
  68.         print("There are two separate functions you can call: ")
  69.         print("StudsTo( number Amount of studs, string Unit you're converting to, bool Canon or Realistic conversion )")
  70.         print'and'
  71.         print("ToStuds( number Amount of your unit, string Unit you're converting to, bool Canon or Realistic conversion )")
  72.         print"Leaving the thrid argument blank will always result in 'Realistic' conversions."
  73.     else
  74.         print('Welcome to the full help guide on converting ROBLOX studs to and from realistic units!')
  75.         print''
  76.         print''
  77.         print("There are two separate functions you can call: ")
  78.         print("StudsTo( number Amount of studs, string Unit you're converting to, bool Canon or Realistic conversion )")
  79.         print'and'
  80.         print("ToStuds( number Amount of your unit, string Unit you're converting to, bool Canon or Realistic conversion )")
  81.         print"Leaving the thrid argument blank will always result in 'Realistic' conversions."
  82.         print''
  83.         print''
  84.         print("You can only convert to and from the following units of measurement: ")
  85.         print("feet, f")
  86.         print("inches, i")
  87.         print("yards, y")
  88.         print("miles, mi")
  89.         print("milimeters, mm")
  90.         print("centimeters, cm")
  91.         print("meters, m")
  92.         print("kilometers, km")
  93.         print''
  94.         print''
  95.     end
  96. end
  97.  
  98. function API.StudsTo(Amount,Unit,Canon)
  99.     DeferToWhat(Canon)
  100.     if API[Ref.."Multipliers"][Unit] == nil then Unit = ParseUnit(Unit) end
  101.     if API[Ref.."Multipliers"][Unit] == nil then
  102.         print("Sorry pal, I can't convert to that unit") API.Help("Unit") return 0.0
  103.     elseif Amount == nil then
  104.         print("Sorry pal, you didn't give me a number...") API.Help("API") return 0.0
  105.     elseif Amount == 0 or math.abs(Amount) >= 4294967295 then
  106.         print("Very funny, nobody can convert that!") API.Help("API") return 0.0
  107.     else
  108.         return Amount/API[Ref.."Multipliers"][Unit]
  109.     end
  110. end
  111.  
  112. function API.ToStuds(Amount,Unit,Canon)
  113.     DeferToWhat(Canon)
  114.     if API[Ref.."Multipliers"][Unit] == nil then Unit = ParseUnit(Unit) end
  115.     if API[Ref.."Multipliers"][Unit] == nil then
  116.         print("Sorry pal, I can't convert from that unit") API.Help("Unit") return 0.0
  117.     elseif Amount == nil then
  118.         print("Sorry pal, you didn't give me a number...") API.Help("API") return 0.0
  119.     elseif Amount == 0 or math.abs(Amount) >= 4294967295 then
  120.         print("Very funny, nobody can convert that!") API.Help("API") return 0.0
  121.     else
  122.         return Amount*API[Ref.."Multipliers"][Unit]
  123.     end
  124. end
  125.  
  126.  
  127. return API
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement