SpacecowboyHX

gwerigj0

Mar 10th, 2021
896
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.62 KB | None | 0 0
  1. local toolbar = plugin:CreateToolbar("Model to Lua")
  2. local selected = game:GetService("Selection")
  3. local history = game:GetService("ChangeHistoryService")
  4. local button = toolbar:CreateButton("Transform", "Transform a model to Lua code", "rbxassetid://6499100516")
  5.  
  6. local version_ = "1.9"
  7.  
  8. local ClassProperties do
  9.  
  10.     ClassProperties = {}
  11.     local HttpService = game:GetService("HttpService")
  12.  
  13.     local Data = HttpService:JSONDecode(HttpService:GetAsync("https://anaminus.github.io/rbx/json/api/latest.json"))
  14.  
  15.     for i = 1, #Data do
  16.         local Table = Data[i]
  17.         local Type = Table.type
  18.  
  19.         if Type == "Class" then
  20.             local ClassData = {}
  21.  
  22.             local Superclass = ClassProperties[Table.Superclass]
  23.  
  24.             if Superclass then
  25.                 for j = 1, #Superclass do
  26.                     ClassData[j] = Superclass[j]
  27.                 end
  28.             end
  29.  
  30.             ClassProperties[Table.Name] = ClassData
  31.         elseif Type == "Property" then
  32.             if not next(Table.tags) then
  33.                 local Class = ClassProperties[Table.Class]
  34.                 local Property = Table.Name
  35.                 local Inserted
  36.  
  37.                 for j = 1, #Class do
  38.                     if Property < Class[j] then -- Determine whether `Property` precedes `Class[j]` alphabetically
  39.                         Inserted = true
  40.                         table.insert(Class, j, Property)
  41.                         break
  42.                     end
  43.                 end
  44.  
  45.                 if not Inserted then
  46.                     table.insert(Class, Property)
  47.                 end
  48.             end
  49.         elseif Type == "Function" then
  50.         elseif Type == "YieldFunction" then
  51.         elseif Type == "Event" then
  52.         elseif Type == "Callback" then
  53.         elseif Type == "Enum" then
  54.         elseif Type == "EnumItem" then
  55.         end
  56.     end
  57. end
  58. function CopyToLua(sub)
  59.     local all = sub:GetChildren()
  60.     local list = {}
  61.     local count = 0
  62.     sub.Name = string.gsub(sub.Name,"[^%l%u_]+","")
  63.     table.insert(list,1,sub)
  64.     local function add(all)
  65.         for i = 1,#all do
  66.             print("adding variables /"..tostring(i).."/ things")
  67.             count = count+1
  68.             if count>25 then
  69.                 wait()
  70.             end
  71.             table.insert(list,#list+1,all[i])
  72.         end
  73.         for i = 1,#all do
  74.             wait()
  75.             print("adding sub-variable /"..tostring(i).."/ things")
  76.             if #(all[i]:GetChildren())>0 then
  77.                 add(all[i]:GetChildren())
  78.             end
  79.         end
  80.     end
  81.     add(all)
  82.     local line = ""
  83.     local cCount = 0
  84.     for i = 1,#list do
  85.         print("making variables /"..tostring(i).."/ things")
  86.         if list[i] then
  87.             local name = list[i].Name
  88.             for x = 1,#list do
  89.                 if list[i].Name==list[x].Name and list[i]~=list[x] then
  90.                     cCount = cCount+1
  91.                     print("renaming variable")
  92.                     name = name..tostring(cCount)
  93.                     list[i].Name = name
  94.                     break
  95.                 end
  96.                 name = string.gsub(name,"[^%l%u_]+","")
  97.                 list[i].Name = name
  98.             end
  99.             line = line..[[
  100. local ]]..name..[[ = Instance.new("]]..list[i].ClassName..[[")]]
  101.         end
  102.     end
  103.     for i = 1,#list do
  104.         wait()
  105.         print("proccessing, /"..tostring(i).."/ things")
  106.         if list[i]~=nil then
  107.             local props = ClassProperties[list[i].ClassName]
  108.             local values = {}
  109.             for a = 1,#props do
  110.                 table.insert(values,a,list[i][props[a]])
  111.             end
  112.             if #props>0 then
  113.                 --line = line.." local "..list[i].Name..[[ = Instance.new("]]..list[i].ClassName..[[", ]]..list[i].Parent.Name..[[)]]
  114.                 for o = 1,#props do
  115.                     local thing = typeof(values[o])
  116.                     local value
  117.                     if #(tostring(values[o]))<1 and props[o] ~= "LinkedSource" then
  118.                         if type(values[o]) == "string" then
  119.                             value = [[" "]]
  120.                         else
  121.                             if type(values[o])=="number" then
  122.                                 value = "0"
  123.                             else
  124.                                 value = "nil"
  125.                             end
  126.                         end
  127.                     else
  128.                         if thing == "Vector3" then
  129.                             print("vector")
  130.                             value = "Vector3.new("..tostring(values[o])..")"
  131.                         else
  132.                             if thing == "Udim2" then
  133.                                 print("ud1m2")
  134.                                 value = "Udim2.new("..tostring(values[o])..")"
  135.                             else
  136.                                 if thing == "Color3" then
  137.                                     print("color3")
  138.                                     value = "Color3.new("..tostring(values[o])..")"
  139.                                 else
  140.                                     if thing == "BrickColor" then
  141.                                         print("brickColor")
  142.                                         value = [[BrickColor.new("]]..tostring(values[o])..[[")]]
  143.                                     else
  144.                                         if thing == "CFrame" then
  145.                                             print("cframe matrix")
  146.                                             value = "CFrame.new("..tostring(values[o].Position)..")*CFrame.Angles("..tostring(values[o].LookVector.Unit)..")"
  147.                                         else
  148.                                             if props[o]=="Name" then
  149.                                                 print("name")
  150.                                                 value = [["]]..tostring(values[o])..[["]]
  151.                                             else
  152.                                                 if string.lower(string.sub(tostring(props[o]),#tostring(props[o])-1,#tostring(props[o])))=="id" or string.lower(props[o]) == "texture" then
  153.                                                     print("texture")
  154.                                                     value = [["]]..tostring(values[o])..[["]]
  155.                                                 else
  156.                                                     --check for rbxassetid://
  157.                                                     if string.lower(string.sub(tostring(values[o]),1,10))=="rbxassetid" then
  158.                                                         print("asset link")
  159.                                                         value = [["]]..tostring(values[o])..[["]]
  160.                                                     else
  161.                                                         if tostring(values[o]) == "inf" then
  162.                                                             print("infinite value")
  163.                                                             value = "math.huge"
  164.                                                         else
  165.                                                             --check for 'value'
  166.                                                             if string.sub(tostring(props[o]),#tostring(props[o])-5,#tostring(props[o]))=="Value" and type(values[o])=="string" then
  167.                                                                 print("string value")
  168.                                                                 value = [["]]..tostring(values[o])..[["]]
  169.                                                             else
  170.                                                                 if string.sub(tostring(props[o]),#tostring(props[o])-7,#tostring(props[o]))=="Surface" then
  171.                                                                     if tostring(values[o])=="nil" then
  172.                                                                         print("surface texture")
  173.                                                                         value = "Enum.SurfaceType.Smooth"
  174.                                                                     end
  175.                                                                 else
  176.                                                                     if props[o] == "CustomPhysicalProperties" then
  177.                                                                         print("physical properties")
  178.                                                                         value = "PhysicalProperties.new("..tostring(values[o])..")"
  179.                                                                     else
  180.                                                                         if props[o] == "LinkedSource" then
  181.                                                                             value = list[i].Source
  182.                                                                         else
  183.                                                                             value = tostring(values[o])
  184.                                                                         end
  185.                                                                     end
  186.                                                                 end
  187.                                                             end
  188.                                                         end
  189.                                                     end
  190.                                                     --lol so many ends
  191.                                                 end
  192.                                             end
  193.                                         end
  194.                                     end
  195.                                 end
  196.                             end
  197.                         end
  198.                     end
  199.                     if tostring(props[o])=="LinkedSource" then
  200.                         warn("Setting script source")
  201.                         line = line..[[
  202. ]]..list[i].Name..".Source = [["..tostring(value).."]]"
  203.                     else
  204.                         if tostring(props[o])~="CustomPhysicalProperties" then
  205.                             line = line..[[
  206. ]]..list[i].Name.."."..props[o].." = "..tostring(value)
  207.                         end
  208.                     end
  209.                 end
  210.             end
  211.         end
  212.     end
  213.     local newCode = Instance.new("Script")
  214.     newCode.Name = sub.Name.." Maker"
  215.     newCode.Parent = workspace
  216.     newCode.Disabled = false
  217.     wait()
  218.     local save = ""
  219.     local worked,msg = pcall(function()
  220.         for i = 1,4 do
  221.             print("Inserting string "..tostring(i))
  222.             wait(0.25)
  223.             local ans = #line/4
  224.             local spot = ((i-1)*ans)+1
  225.  
  226.             newCode.Source = newCode.Source..string.sub(line,spot,spot+(ans-1))
  227.             save = newCode.Source..string.sub(line,spot,spot+(ans-1))
  228.         end
  229.     end)
  230.     if worked == false then
  231.         warn("Error at "..tostring(math.floor(tick())).." : "..msg..", attempted to make backup save.")
  232.         return save, newCode
  233.     end
  234. end
  235. function Clicked()
  236.     local selection = selected:Get()
  237.     if #selection==1 then
  238.         warn("Starting Conversion "..version_.."...")
  239.         wait(2)
  240.         local save,code
  241.         local worked, msg = pcall(function()
  242.             save,code = CopyToLua(selection[1])
  243.         end)
  244.         if worked == false then
  245.             warn("Error at "..tostring(math.floor(tick())).." : "..msg)
  246.             pcall(function()
  247.                 if save and code then
  248.                     code.Parent = workspace
  249.                     code.Source = save
  250.                     code.Name = "Backup save: "..code.Name
  251.                 end
  252.             end)
  253.         end
  254.     end
  255. end
  256. button.Click:Connect(Clicked)
Advertisement
Add Comment
Please, Sign In to add comment