Advertisement
StefanBashkir

RBX.Lua Functions Library

Dec 22nd, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.23 KB | None | 0 0
  1. --// ROBLOX-Lua function library
  2. --[[ #Documentation
  3.     GetDescendants(instance Parent)
  4.         <- Returns a table with all the descendants of 'Parent.'
  5.     RemoveFromTable(table Table, variant Value, [,bool RemoveAllOccurrences = false])
  6.         <- Returns a table with the same structure as the given one, but without 'Value' in it.
  7.         <- Does not recurse on tables within tables
  8.         <- Will remove any occurrence of 'Value' UNLESS RemoveAllOccurrences is set to false
  9.     FindInTable(table Table, variant Value, [,bool Recurse = false])
  10.         <- Returns true if 'Value' is found in 'Table'
  11.         <- Optional 'Recurse' parameter to recurse through all descendant tables
  12.     GetAllDirections(instance BasePart)
  13.         <- Returns a table of the directions each side of the part is facing.
  14.         <- Format of returned table is:
  15.             {
  16.                 ['up'] = Vector3;
  17.                 ['down'] = Vector3;
  18.                 ['left'] = Vector3;
  19.                 ['right'] = Vector3;
  20.                 ['front'] = Vector3;
  21.                 ['back'] = Vector3;
  22.             }
  23.     GetSurfaceClosestToPoint(instance BasePart, vector3 Position)
  24.         <- Will return a NormalId enum which is the side of the 'BasePart' that is closest to 'Position'
  25.     GetFormattedRegion3(vector3 Corner, vector3 Corner)
  26.         <- Will return a correctly-formatted Region3 value given these coordinates.
  27.         <- None of the coordinates (x,y,z) can be the same as a co-existing x,y,z of any given vector.
  28.             <- this means, for exame, x of the first vector cannot be equal to x of the second or vice versa
  29.         <- Why is this needed? Typically, you would need to format the first vector with being the smallest numbers; and the second vector having the largest. Check the source of the function for the Lua example.
  30.        
  31. ]]
  32.  
  33. function GetDescendants(instance)
  34.     getfenv()['_d'] = {};
  35.     if (type(instance) ~= "userdata") or (not pcall(function() return instance['Name'] end)) then
  36.         error("GetDescendants requires an object.");
  37.     end
  38.     local function Get(parent)
  39.         for i,v in pairs(parent:GetChildren()) do
  40.             if (#v:GetChildren() > 0) then
  41.                 Get(v);
  42.             end
  43.             table.insert(_d, v)
  44.         end
  45.     end
  46.     Get(instance);
  47.     return _d;
  48. end
  49.  
  50. function RemoveFromTable(t, val, rac)
  51.     local new_t = {};
  52.     local found = 0;
  53.     local adding = true;
  54.     local rac = (rac ~= nil) and rac or false;
  55.     if (type(rac) ~= "boolean") then
  56.         error("Third argument of RemoveFromTable must be a boolean.");
  57.     end
  58.     if (not val) then
  59.         error("Nil given for value.");
  60.     end
  61.     if
  62.     for i,v in pairs(t) do
  63.         if (v ~= val) then
  64.             table.insert(new_t, v)
  65.         else
  66.             found = found + 1;
  67.             if (not rac) and (adding) then
  68.                 adding = false;
  69.             end
  70.             if (not rac) and (adding) then
  71.                 table.insert(new_t, v)
  72.             end
  73.         end
  74.     end
  75.     return new_t
  76. end
  77.  
  78. function FindInTable(t, val, recurse)
  79.     if (type(recurse) ~= "boolean") then
  80.         error("Third argument of FindInTable must be a boolean.");
  81.     end
  82.     local recurse = (recurse ~= nil) and recurse or false;
  83.     local item;
  84.     local function Search(_t)
  85.         for i,v in pairs(_t) do
  86.             if (item) then break end
  87.             if (type(v) == "table") and (recurse) and (v ~= val) then
  88.                 Search(v)
  89.             elseif (v == val) then
  90.                 item = v;
  91.             end
  92.         end
  93.     end
  94.     Search(t)
  95.     if (item) then
  96.         return true;
  97.     end
  98. end
  99.  
  100. function GetAllDirections(instance)
  101.     local directions = {};
  102.     if (type(instance) ~= "userdata") or (not pcall(function() return instance['Name'] end)) then
  103.         error("GetAllDirections requires an object.");
  104.     end
  105.     local matrix = instance.CFrame;
  106.     directions['up'] =
  107.         matrix:vectorToWorldSpace( Vector3.new(0,1,0) );
  108.     directions['down'] =
  109.         matrix:vectorToWorldSpace( Vector3.new(0,-1,0) );
  110.     directions['left'] =
  111.         matrix:vectorToWorldSpace( Vector3.new(-1,0,0) );
  112.     directions['right'] =
  113.         matrix:vectorToWorldSpace( Vector3.new(1,0,0) );
  114.     directions['front'] =
  115.         matrix.lookVector;
  116.     directions['back'] =
  117.         -matrix.lookVector;
  118.     return directions;
  119. end
  120.  
  121. function GetSurfaceClosestToPoint(part, pos)
  122.     if (type(part) ~= "userdata") or (not pcall(function() return part['Name'] end)) then
  123.         error("GetSurfaceClosestToPoint requires an object.");
  124.     end
  125.     local _, count = tostring(pos):gsub(",",",");
  126.     if (type(pos) ~= "userdata" or (count ~= 2) then
  127.         error("Argument 2 for GetSurfaceClosestToPoint must be a Vector3.");
  128.     end
  129.     local rel = part.CFrame:pointToObjectSpace(pos)
  130.     if (math.abs(rel.Z) > math.abs(rel.Y)) and (math.abs(rel.Z) > math.abs(rel.X)) then
  131.         if (rel.Z > 0) then
  132.             return Enum.NormalId.Back
  133.         else
  134.             return Enum.NormalId.Front
  135.         end
  136.     elseif (math.abs(rel.Y) > math.abs(rel.Z)) and (math.abs(rel.Y) > math.abs(rel.X)) then
  137.         if (rel.Y > 0) then
  138.             return Enum.NormalId.Top
  139.         else
  140.             return Enum.NormalId.Bottom
  141.         end
  142.     elseif (math.abs(rel.X) > math.abs(rel.Z) and math.abs(rel.X) > math.abs(rel.Y)) then
  143.         if (rel.X > 0) then
  144.             return Enum.NormalId.Right
  145.         else
  146.             return Enum.NormalId.Left
  147.         end
  148.     end
  149. end
  150.  
  151. function GetFormattedRegion3(v3,_v3)
  152.     if (v3.x == _v3.x) or (v3.y == _v3.y) or (v3.z == _v3.z) then
  153.         error("GetFormattedRegion3 cannot format a Region3 given these coordinates. One or more of the x,y or z values is equal to that of the opposite argument.");
  154.     end
  155.     return Region3.new(
  156.         Vector3.new(
  157.             math.min(v3.x, _v3.x),
  158.             math.min(v3.y,_v3.y),
  159.             math.min(v3.z,_v3.z)
  160.         ),
  161.         Vector3.new(
  162.             math.max(v3.x, _v3.x),
  163.             math.max(v3.y,_v3.y),
  164.             math.max(v3.z,_v3.z)
  165.         )
  166.     )
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement