ForeverDev

Untitled

Sep 26th, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.16 KB | None | 0 0
  1. --[[
  2.     Studio+ by ForeverDev.  Project started September 23rd, 2014.
  3.    
  4.     What is Studio+ ?
  5.         Studio+ is a module script designed to allow more scripting in fewer lines.  It
  6.         has a very simplistic syntax.  One feature of Studio+ is the use of table manipulation.
  7.         Studio+ allows you to manipulate tables in many different ways.  Consider the following code
  8.        
  9.             local array = {1, 3, 5, 7, 9, 11}
  10.             sp(array).drop(1, 5, 7)
  11.             print(table.concat(array, ", ")) --> 3, 9, 11
  12.            
  13.            
  14.         Studio+ also allows you to easily edit objects.  For example, the following code changes a
  15.         frame's border, background, and text color to red.
  16.        
  17.             sp(frame).change("BackgroundColor3", "TextColor3", "BorderColor3", Color3.new(1, 0, 0))
  18.            
  19.         Isn't that a lot easier to read and better looking than the following?
  20.        
  21.             frame.BackgroundColor3 = Color3.new(1, 0, 0)
  22.             frame.TextColor3 = Color3.new(1, 0, 0)
  23.             frame.BorderColor3 = Color3.new(1, 0, 0)
  24.            
  25.     How do I use Studio+ ?
  26.         It's very easy to enable Studio+ in your script.  First, make sure this module script
  27.         is located inside of ServerScriptService.  To allow any script to use Studio+, place
  28.         the following line of code at the top of any script:
  29.            
  30.             local sp = require(game.ServerScriptService.StudioPlus)
  31.            
  32.         If you want to edit the properties of an object, you first have to 'point' to it.  To
  33.         do this, you would use the following code in your script:
  34.        
  35.             sp(PATH_TO_OBJECT)
  36.            
  37.         For example, if you wanted to 'point' to the BasePlate, you would do the following:
  38.        
  39.             sp(Workspace.BasePlate)
  40.            
  41.         Then, you can edit the BasePlate.  You can use as many SP (Studio+) functions as you
  42.         want in a row, as long as you don't interrupt it with other Lua code.
  43.        
  44.             sp(Workspace.BasePlate)
  45.                 .val("hi")
  46.                 .change("Transparency", 1)
  47.                
  48.         Here is an example of how NOT to use SP.
  49.        
  50.             sp(Workspace.BasePlate)
  51.                 .val("hi")
  52.             print("hello!")                         <-- this print line will cause an error
  53.                 .change("Transparency", 1) 
  54.                
  55.                
  56.         As stated earlier, SP can perform many different functions on tables.  To
  57.         manipulate a table with SP, you 'point' to a table the same way you point to an
  58.         object.  For example:
  59.        
  60.             local array = {1, 3, 5, 7}
  61.            
  62.             local median = sp(array).median()
  63.             local sum = sp(array).add()
  64.            
  65.             print(median) --> 4
  66.             print(sum) --> 16
  67.            
  68.         Finally, SP can handle events.  The following code would execute when a player joins the game:
  69.        
  70.             sp(game.Players).newPlayer(function(player) print(player.Name) end)
  71.            
  72.         All SP functions are listed below.
  73.            
  74. -------------------------------------------------------------------------------------------------------------------------
  75.                
  76.     Current SP functions :
  77.    
  78.     TABLE FUNCTIONS:
  79.         --> .add()
  80.                 returns the sum of all numbers inside of the table
  81.         --> .average()
  82.                 returns the average of all numbers inside of the table
  83.         --> .concat(String split)
  84.                 returns the table concatenated with each element separated by split
  85.         --> .drop(...)
  86.                 removes all given values from the table
  87.         --> .getRandom()
  88.                 returns a random value from the table
  89.         --> .insert(...)
  90.                 inserts all given values into the table
  91.         --> .median()
  92.                 retuns the median of all numbers inside of the table
  93.         --> .multiply()
  94.                 returns the product of all numbers inside of the table
  95.         --> .order()
  96.                 sorts all numbers inside of the table by value
  97.         --> .shuffle()
  98.                 moves table[1] to table[#table], and moves each other index up one
  99.                 ex) sp({1, 2, 3, 4, 5}).shuffle() == {2, 3, 4, 5, 1}
  100.                
  101.     STRING FUNCTIONS:
  102.         --> .cut(...)
  103.                 removes any passed pattern found in the string
  104.         --> .find(String pattern)
  105.                 returns first index of found pattern, last index of found pattern, and any captures
  106.         --> .gmatch(String pattern, Function patternFound[ARGS: MATCH])
  107.                 returns an iteration function, calls patternFound if the pattern is found in the string.  ex:
  108.                 sp("hello").gmatch("%a", function(match)
  109.                     print(match)
  110.                 end)
  111.         --> .iterate(String pattern, String/Function replace)
  112.                 replaces any found patterns with replace
  113.         --> .match(String pattern)
  114.                 returns a substring of the found pattern
  115.         --> .sub(Integer start, Integer end)
  116.                 returns string.substring(string, start, end)
  117.                
  118.     OBJECT FUNCTIONS:
  119.         --> .change(String property1, ..., value)
  120.                 changes all the given properties to the last argument
  121.         --> .child(String childName)
  122.                 returns true if a child named childName is located inside of object.  
  123.                 does not return an instance like :FindFirstChild()
  124.         --> .destroy(Function f[ARGS: NONE])
  125.                 destroys the object, executes f if provided (basically an object.onDestroy event)
  126.         --> .move(Vector3/CFrame location)
  127.                 moves the object (also works with models) to the given Vector3 location.
  128.         --> .val(String value)
  129.                 creates a string value inside of object with a value of value
  130.         --> .val()
  131.                 returns the value inside of an object, or nil
  132.                
  133.     OBJECT EVENTS:
  134.         --> .changed(Function f[ARGS: String property])
  135.                 executes f when a property of object is changed
  136.         --> .clicked(Function f[ARGS: NONE])
  137.                 executes f when object is clicked
  138.         --> .childAdded(Function f[ARGS: Instance child])
  139.                 executes f when a child is added to object.  'child' is the new child
  140.         --> .childRemoved(Function f[ARGS: Instance child])
  141.                 executes f when a child under object is removed.  'child' is the instance that was removed
  142.         --> .mouseEnter(Function f[ARGS: NONE])
  143.                 executes f when the player hovers their mouse over a GUI
  144.         --> .mouseLeave(Function f[ARGS: NONE])
  145.                 executes f when the players takes their mouse off of a GUI
  146.         --> .playerAdded(Function f[ARGS: Instance player])
  147.                 executes f when a player is added.  only works if object is game.Players.  'player' is the new player
  148.         --> .playerLeaving(Function f[ARGS: Instance player])
  149.                 executes f when a player leaves.  only works if object is game.Players.  'player' is the player that left
  150.         --> .renderStep(Function f[ARGS: NONE])
  151.                 executes sixty times per second, only works in a local script
  152. ]]
  153.  
  154. local patterns = {
  155.     number = "%-?[%d+%.]+",
  156.     phrase = "[%a?%d?]+",
  157.     space = "%s+",
  158.     word = "%a+",
  159. }
  160.  
  161. function translate(language, code)
  162.     local translations;
  163.     if language == "javascript" or language == "js" then
  164.         translations = {
  165.             ["(%a+)%s*%+%+"] = "%1 = %1 + 1",
  166.             ["(%a+)%s*%+%=%s*(%-?[%d+%.]+)"] = "%1 = %1 + %2",
  167.             ["(%a+)%s*%-%-"] = "%1 = %1 - 1",          
  168.             ["if%s*%((.+)%)%s*{"] = "if %1 then",
  169.             ["while%s*%((.+)%)%s*{"] = "while %1 do",  
  170.             ["for%s*%(v?a?r?%s*([%a?%d?]+%s*%=%s*%-?[%d+%.]+);%s*[%a?%d?]+.+(%-?[%d+%.]+);.+(%-?[%d+%.]+)%s*%)%s*{"] = "for %1, %2, %3 do",
  171.             ["for%s*%(v?a?r?%s*([%a?%d?]+%s*%=%s*%-?[%d+%.]+);%s*[%a?%d?]+%s*.%s*([%a?%d?]+);%s*[%a?%d?]+%s*=%s*[%a?%d?]+%s*.%s*(%-?[%d+%.]+)%s*%)%s*{"] = "for %1, %2, %3 do",
  172.             ["function%s+(%a+)%s*%((.+)%)%s*{"] = "function %1(%2)",
  173.             ["(function%s+%a+%s*%(.+%).+)}"] = "%1 end",
  174.             ["([while]?[if]?)(.+)([do]?[then]?)(.+)}"] = "%1 %2 %3 %4 end",
  175.             ["console%.log"] = "print",    
  176.             ["var"] = "local"
  177.         }
  178.     end
  179.     for pattern, replace in pairs(translations) do
  180.         code = code:gsub(pattern, replace)
  181.     end
  182.     return code
  183. end
  184.  
  185. return function(object)
  186.     local studio;
  187.     studio = {
  188.         --begin table functions
  189.         add = function()
  190.             if type(object) == "table" then
  191.                 local sum = 0
  192.                 for _, number in ipairs(object) do
  193.                     sum = sum + number
  194.                 end
  195.                 return sum
  196.             end
  197.         end,
  198.         multiply = function()
  199.             if type(object) == "table" then
  200.                 local product = 1
  201.                 for _, number in ipairs(object) do
  202.                     product = product * number
  203.                 end
  204.                 return product
  205.             end
  206.         end,
  207.         average = function()
  208.             if type(object) == "table" then
  209.                 local average = 0
  210.                 for _, number in ipairs(object) do
  211.                     average = average + number
  212.                 end
  213.                 return average / #object
  214.             end
  215.         end,
  216.         shuffle = function()
  217.             if type(object) == "table" then
  218.                 local shift = {}
  219.                 for index, value in ipairs(object) do
  220.                     if index == 1 then
  221.                         shift[#object] = value
  222.                     else
  223.                         shift[index - 1] = value
  224.                     end
  225.                 end
  226.                 object = shift
  227.                 return object
  228.             end
  229.         end,
  230.         median = function()
  231.             if type(object) == "table" then
  232.                 local replicated = object
  233.                 table.sort(replicated)
  234.                 repeat
  235.                     table.remove(replicated, 1)
  236.                     table.remove(replicated, #replicated)
  237.                 until #replicated <= 2
  238.                 return #replicated == 2 and (replicated[1] + replicated[2]) / 2 or replicated[1]
  239.             end
  240.         end,
  241.         drop = function(...)
  242.             if type(object) == "table" then
  243.                 for index, value in ipairs(object) do
  244.                     for _, search in ipairs({...}) do
  245.                         if search == value then
  246.                             table.remove(object, index)
  247.                         end
  248.                     end
  249.                 end
  250.                 return object
  251.             end
  252.         end,
  253.         insert = function(...)
  254.             if type(object) == "table" then
  255.                 for _, element in ipairs({...}) do
  256.                     table.insert(object, element)
  257.                 end
  258.             end
  259.         end,
  260.         getRandom = function()
  261.             if type(object) == "table" then
  262.                 return object[math.random(#object)]
  263.             end
  264.         end,
  265.         concat = function(between)
  266.             if type(object) == "table" then
  267.                 return table.concat(object, between)
  268.             end
  269.         end,
  270.         order = function()
  271.             if type(object) == "table" then
  272.                 table.sort(object)
  273.             end
  274.             return object
  275.         end,
  276.        
  277.         --begin string functions
  278.         sub = function(begin, ending)
  279.             return tostring(object):sub(begin, ending)
  280.         end,
  281.         find = function(pattern)
  282.             return tostring(object):find(patterns[pattern] or pattern)
  283.         end,
  284.         match = function(pattern)
  285.             return tostring(object):match(patterns[pattern] or pattern)
  286.         end,
  287.         gmatch = function(pattern, func)
  288.             for match in tostring(object):gmatch(patterns[pattern] or pattern) do
  289.                 func(match)
  290.             end
  291.         end,
  292.         iterate = function(pattern, func)
  293.             object = tostring(object):gsub(patterns[pattern] or pattern, func)
  294.             return object
  295.         end,
  296.         cut = function(...)
  297.             for _, pattern in ipairs({...}) do
  298.                 object:gsub(patterns[pattern] or pattern, "")
  299.             end
  300.         end,
  301.         executeLanguage = function(language)
  302.             local translation = translate(language, tostring(object))
  303.             print(translation)
  304.             loadstring(translate(language, tostring(object)))()
  305.         end,
  306.        
  307.         --begin object functions
  308.         val = function(value)
  309.             if not value then
  310.                 return object:FindFirstChild("Storage") and object.Storage.Value or nil
  311.             elseif not object:FindFirstChild("Storage") then
  312.                 Instance.new("StringValue", object).Name = "Storage"
  313.             end
  314.             object.Storage.Value = tostring(value)
  315.             return studio
  316.         end,
  317.         change = function(...)
  318.             local args = {...}
  319.             for index, property in ipairs(args) do
  320.                 if index == #args then
  321.                     break
  322.                 end
  323.                 object[property] = args[#args]
  324.             end
  325.             return studio
  326.         end,
  327.         move = function(position)
  328.             if object:IsA("BasePart") then
  329.                 object.Position = position
  330.             elseif object:IsA("Model") then
  331.                 object:MoveTo(position)
  332.             end
  333.             return studio
  334.         end,
  335.         destroy = function(event)
  336.             if event then
  337.                 event(object)
  338.             end
  339.             object:Destroy()
  340.             return studio
  341.         end,
  342.         child = function(name)
  343.             return object:FindFirstChild(name)
  344.         end,
  345.        
  346.         --begin object functions
  347.         clicked = function(func)
  348.             if object:IsA("TextButton") or object:IsA("ImageButton") then
  349.                 object.MouseButton1Click:connect(func)
  350.             elseif object:IsA("ClickDetector") then
  351.                 object.MouseClick:connect(func)
  352.             end
  353.             return studio
  354.         end,
  355.         mouseEnter = function(func)
  356.             object.MouseEnter:connect(func)
  357.             return studio
  358.         end,
  359.         mouseLeave = function(func)
  360.             object.MouseLeave:connect(func)
  361.             return studio
  362.         end,
  363.         childAdded = function(func)
  364.             object.ChildAdded:connect(func)
  365.             return studio
  366.         end,
  367.         childRemoved = function(func)
  368.             object.ChildRemoved:connect(func)
  369.             return studio
  370.         end,
  371.         changed = function(func)
  372.             object.Changed:connect(func)
  373.             return studio
  374.         end,
  375.         playerAdded = function(func)
  376.             if object:IsA("Players") then
  377.                 object.PlayerAdded:connect(func)
  378.             end
  379.             return studio
  380.         end,
  381.         playerLeaving = function(func)
  382.             if object:IsA("Players") then
  383.                 object.PlayerRemoving:connect(func)
  384.             end
  385.             return studio
  386.         end,
  387.         renderStep = function(func)
  388.             game:GetService("RunService").RenderStepped:connect(func)
  389.             return studio
  390.         end
  391.     }
  392.     return studio
  393. end
Advertisement
Add Comment
Please, Sign In to add comment