File size: 1,889 Bytes
0712d5f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | -- src/ReplicatedStorage/Shared/Utility.lua
local Utility = {}
function Utility.formatCash(amount)
if amount >= 1000000 then
return string.format("$%.1fM", amount / 1000000)
elseif amount >= 1000 then
return string.format("$%.1fK", amount / 1000)
else
return "$" .. tostring(math.floor(amount))
end
end
function Utility.formatTime(seconds)
local min = math.floor(seconds / 60)
local sec = math.floor(seconds % 60)
return string.format("%d:%02d", min, sec)
end
function Utility.lerp(a, b, t)
return a + (b - a) * t
end
function Utility.getPartVolume(part)
if not part or not part:IsA("BasePart") then return 0 end
return part.Size.X * part.Size.Y * part.Size.Z
end
function Utility.randomInRange(min, max)
return min + math.random() * (max - min)
end
function Utility.randomIntInRange(min, max)
return math.random(min, max)
end
function Utility.clampVector3(vec, minBounds, maxBounds)
return Vector3.new(
math.clamp(vec.X, minBounds.X, maxBounds.X),
math.clamp(vec.Y, minBounds.Y, maxBounds.Y),
math.clamp(vec.Z, minBounds.Z, maxBounds.Z)
)
end
function Utility.shuffleTable(tbl)
local shuffled = {}
for _, v in ipairs(tbl) do
table.insert(shuffled, v)
end
for i = #shuffled, 2, -1 do
local j = math.random(1, i)
shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
end
return shuffled
end
function Utility.tableContains(tbl, value)
for _, v in ipairs(tbl) do
if v == value then return true end
end
return false
end
function Utility.tableKeys(tbl)
local keys = {}
for k, _ in pairs(tbl) do
table.insert(keys, k)
end
return keys
end
function Utility.deepCopy(original)
local copy = {}
for k, v in pairs(original) do
if type(v) == "table" then
copy[k] = Utility.deepCopy(v)
else
copy[k] = v
end
end
return copy
end
return Utility
|