Guest User

cabbler functions

a guest
Jun 4th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --Simplest full scan of an object's hierarchy
  2.  
  3. function scan(x) if true then end for _,v in pairs(x:GetChildren()) do scan(v) end end
  4. scan(workspace) --anything
  5.  
  6. --Linear sort
  7.  
  8. function sort(tab)
  9.     local sorted = {}
  10.     for _,a in pairs(tab) do
  11.         local index = #sorted+1
  12.         for i,b in pairs(sorted) do
  13.             if a < b then
  14.                 index = i
  15.                 break
  16.             end
  17.         end
  18.         table.insert(sorted,index,a)
  19.     end
  20.     return sorted
  21. end
  22.  
  23. --Random float, self explanatory
  24.  
  25. function randFloat(min,max)
  26.     min, max = min or 1, max or 0
  27.     return min + math.random()*(max-min)
  28. end
  29.  
  30. --Rounding
  31.  
  32. function round(num,place)
  33.     if place then
  34.         return math.floor(num * 10^place + .5) / 10^place
  35.     else
  36.         return math.floor(num+.5)
  37.     end
  38. end
  39.  
  40. --Great tostring for tables, so useful. Can stylize however you want actually.
  41.  
  42. function printTable(tab,isArray)
  43.     local str = "{"
  44.     if isArray then
  45.         for i=1,#tab do
  46.             str = str .. " " .. tostring(tab[i]) .. ","
  47.         end
  48.     else
  49.         for i,v in pairs(tab) do
  50.             str = str .. " [" .. tostring(i) .. "]: " .. tostring(v) .. ","
  51.         end
  52.     end
  53.     str = str:sub(1,math.max(1,#str-1)) .. " }"
  54.     print(str)
  55. end
  56.  
  57. --Fisher Yates algorithm I got off google
  58.  
  59. function shuffle(array)
  60.     local output = { }
  61.     local random = math.random
  62.  
  63.     for index = 1, #array do
  64.         local offset = index - 1
  65.         local value = array[index]
  66.         local randomIndex = offset*random()
  67.         local flooredIndex = randomIndex - randomIndex%1
  68.  
  69.         if flooredIndex == offset then
  70.             output[#output + 1] = value
  71.         else
  72.             output[#output + 1] = output[flooredIndex + 1]
  73.             output[flooredIndex + 1] = value
  74.         end
  75.     end
  76.  
  77.     return output
  78. end
  79.  
  80. --Player from part, good for noobs
  81.  
  82. function GetPlayerFromPart(part)
  83.     local char = part.Parent
  84.     if char:FindFirstChild('Humanoid') then
  85.         return game.Players:GetPlayerFromCharacter(char)
  86.     end
  87. end
  88.  
  89. --Very useful to loop through players you know have at least semi loaded characters. can customize
  90. --team parameter OPTIONAL
  91.  
  92. function GetReadyPlayers(team)
  93.     local players = {}
  94.     for _,p in pairs( (team or game.Players):GetPlayers() ) do
  95.         local char = p.Character
  96.         if char and char.PrimaryPart and char:FindFirstChild('HumanoidRootPart') and char:FindFirstChild('Humanoid') and char.Humanoid.Health>0 then
  97.             table.insert(players,p)
  98.         end
  99.     end
  100.     return players
  101. end
  102.  
  103. --Good for any touch function for PvP or any small parts; fights lag
  104.  
  105. function areTouching(pos1,pos2,dist)
  106.     return (pos2 - pos1).magnitude < (dist or 9)
  107. end
Add Comment
Please, Sign In to add comment