Advertisement
Guest User

Untitled

a guest
Jul 5th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.53 KB | None | 0 0
  1. -- global 'plugin'
  2. local toolbar = plugin:CreateToolbar("Waypoint Creator");
  3. local isPluginActive = false;
  4. local shiftDown = false;
  5. local newPointButton_isActive = false;
  6. local connectPointsButton_isActive = false;
  7. local newPointButton = toolbar:CreateButton(
  8.     "New Waypoint",
  9.     "Click on a surface to create a waypoint piece there.",
  10.     "http://www.roblox.com/asset/?id=1109508861"
  11. );
  12. local connectPointsButton = toolbar:CreateButton(
  13.     "Connect Waypoints",
  14.     "Click a waypoint, then click another different waypoint to connect them.",
  15.     "http://www.roblox.com/asset/?id=1109509029"
  16. );
  17. local connections = {};
  18.  
  19. local folder;
  20.  
  21. local function generateWaypoint(pos)
  22.     if (not folder or not folder.Parent) then
  23.         print("Cannot create a waypoint. There is no folder named Plugin_Waypoints in the Workspace!");
  24.         return
  25.     end
  26.    
  27.     local point = Instance.new("Part")
  28.     point.Size = Vector3.new(3,1,3)
  29.     point.BrickColor = BrickColor.new("Really blue")
  30.     point.Name = "Waypoint"
  31.    
  32.     local mesh = Instance.new("SpecialMesh", point)
  33.     mesh.MeshType = "FileMesh"
  34.     mesh.MeshId = "http://www.roblox.com/Asset/?id=9756362"
  35.     mesh.Scale = Vector3.new(3,3,3)
  36.    
  37.     point.Anchored = true
  38.     point.CanCollide = false
  39.     point.TopSurface = "Smooth"
  40.     point.BottomSurface = "Smooth"
  41.     point.CFrame = CFrame.new(pos)
  42.     point.Parent = folder
  43.     return point
  44. end
  45.  
  46. local function removeConnection(p1,p2)
  47.     for i,v in pairs(p1:GetChildren()) do
  48.         if (v:IsA("ObjectValue")) then
  49.             if (v.Value == p2) then
  50.                 if (v:FindFirstChild("RodValue")) then
  51.                     v.RodValue.Value:Destroy()
  52.                 end
  53.                 v:Destroy()
  54.                 break
  55.             end
  56.         end
  57.     end
  58.     for i,v in pairs(p2:GetChildren()) do
  59.         if (v:IsA("ObjectValue")) then
  60.             if (v.Value == p1) then
  61.                 if (v:FindFirstChild("RodValue")) then
  62.                     v.RodValue.Value:Destroy()
  63.                 end
  64.                 v:Destroy()
  65.                 break
  66.             end
  67.         end
  68.     end
  69. end
  70.  
  71. local function determineIfConnected(p1,p2)
  72.     local c1, c2 = false, false;
  73.     for i,v in pairs(p1:GetChildren()) do
  74.         if (v:IsA("ObjectValue")) then
  75.             if (v.Value == p2) then
  76.                 c1 = true
  77.                 break
  78.             end
  79.         end
  80.     end
  81.     for i,v in pairs(p2:GetChildren()) do
  82.         if (v:IsA("ObjectValue")) then
  83.             if (v.Value == p1) then
  84.                 c2 = true
  85.                 break
  86.             end
  87.         end
  88.     end
  89.     print("Not connected.");
  90.     return (c1 == true and c2 == true);
  91. end
  92.  
  93. local function connectWaypoints(p1, p2)
  94.     if (determineIfConnected(p1,p2) == false) then
  95.         local obv1 = Instance.new("ObjectValue", p1)
  96.         obv1.Name = "Connection"
  97.         obv1.Value = p2
  98.         local obv2 = Instance.new("ObjectValue", p2)
  99.         obv2.Name = "Connection"
  100.         obv2.Value = p1
  101.        
  102.         local rod = Instance.new("Part")
  103.         local distance = (p2.Position - p1.Position).magnitude
  104.         rod.BrickColor = BrickColor.new("New Yeller")
  105.         rod.CanCollide = false;
  106.         rod.Anchored = true;
  107.         rod.Name = "Rod"
  108.         rod.Size = Vector3.new(0.25,0.25,distance)
  109.         rod.CFrame = CFrame.new(p1.Position, p2.Position)
  110.         rod.CFrame = rod.CFrame * CFrame.new(0,0,-distance/2);
  111.         rod.Parent = folder;
  112.        
  113.         local rodValue = Instance.new("ObjectValue", obv1)
  114.         rodValue.Name = "RodValue"
  115.         rodValue.Value = rod;
  116.        
  117.         p1.BrickColor = BrickColor.new("Really blue");
  118.         p2.BrickColor = BrickColor.new("Really blue");
  119.     end
  120.    
  121.     if (p1:FindFirstChild("Fire")) then
  122.         p1.Fire:Destroy()
  123.     end
  124.    
  125.     if (p2:FindFirstChild("Fire")) then
  126.         p2.Fire:Destroy()
  127.     end
  128. end
  129.  
  130. local function removeReferences(waypoint)
  131.     for i,v in pairs(waypoint:GetChildren()) do
  132.         if (v:IsA("ObjectValue")) then
  133.             if (v:FindFirstChild("RodValue")) then
  134.                 v.RodValue.Value:Destroy();
  135.             end
  136.             local part = v.Value;
  137.             for k, j in pairs(part:GetChildren()) do
  138.                 if (j:IsA("ObjectValue")) then
  139.                     if (j.Value == waypoint) then
  140.                         if (j:FindFirstChild("RodValue")) then
  141.                             j.RodValue.Value:Destroy();
  142.                         end
  143.                         j:Destroy()
  144.                     end
  145.                 end
  146.             end
  147.         end
  148.     end
  149. end
  150.  
  151. local function off()
  152.     isPluginActive = false;
  153.     newPointButton_isActive = false;
  154.     newPointButton:SetActive(false);
  155.     connectPointsButton:SetActive(false);
  156.     connectPointsButton_isActive = false;
  157.    
  158.     for i,v in pairs(connections) do
  159.         v:disconnect()
  160.     end
  161.    
  162.     connections = {};
  163. end
  164.  
  165. local function saveProgress()
  166.     local pointName = tostring(os.time());
  167.     game:GetService("ChangeHistoryService"):SetWaypoint(pointName);
  168. end
  169.  
  170. local function fire(waypoint)
  171.     local fire = Instance.new("Fire", waypoint)
  172. end
  173.  
  174. connectPointsButton.Click:connect(function()
  175.     spawn(function()
  176.         isPluginActive = not isPluginActive;
  177.        
  178.         if (isPluginActive) then
  179.             plugin:Activate(true);
  180.             isPluginActive = true;
  181.         end
  182.        
  183.         connectPointsButton_isActive = true;
  184.         connectPointsButton:SetActive(true);
  185.         if (isPluginActive) then
  186.             local mouse = plugin:GetMouse();
  187.             local currentPaintedTarget;
  188.             local waypoint1;
  189.            
  190.             table.insert(connections, mouse.Button1Down:connect(function()
  191.                 if (mouse.Target) then
  192.                     if (shiftDown == true) then
  193.                         if (mouse.Target.Parent == folder and mouse.Target.Name == "Waypoint") then
  194.                             saveProgress();
  195.                             removeReferences(mouse.Target);
  196.                             mouse.Target:Destroy();
  197.                         end
  198.                     else
  199.                         if (mouse.Target.Parent == folder and mouse.Target.Name == "Waypoint") then
  200.                             if not (waypoint1) then
  201.                                 fire(mouse.Target)
  202.                                 waypoint1 = mouse.Target
  203.                             else
  204.                                 connectWaypoints(waypoint1, mouse.Target)
  205.                                 waypoint1 = nil
  206.                                 saveProgress();
  207.                             end
  208.                         end
  209.                     end
  210.                 end
  211.             end));
  212.            
  213.             table.insert(connections, mouse.Move:connect(function()
  214.                 if (currentPaintedTarget) then
  215.                     if (mouse.Target ~= currentPaintedTarget) then
  216.                         currentPaintedTarget.BrickColor = BrickColor.new("Really blue")
  217.                         currentPaintedTarget = nil;
  218.                     end
  219.                 else
  220.                     if (mouse.Target) then
  221.                         if (shiftDown) then
  222.                             if (mouse.Target.Parent == folder and mouse.Target.Name == "Waypoint") then
  223.                                 currentPaintedTarget = mouse.Target;
  224.                                 currentPaintedTarget.BrickColor = BrickColor.new("Really red");
  225.                             end
  226.                         else
  227.                             if (mouse.Target.Parent == folder and mouse.Target.Name == "Waypoint") then
  228.                                 currentPaintedTarget = mouse.Target;
  229.                                 currentPaintedTarget.BrickColor = BrickColor.new("Lime green");
  230.                             end
  231.                         end
  232.                     end
  233.                 end
  234.             end));
  235.            
  236.             table.insert(connections, mouse.KeyDown:connect(function(key)
  237.                 local keyCode = key:byte()
  238.                 if (keyCode == 48) then
  239.                     shiftDown = true;
  240.                 end
  241.             end));
  242.            
  243.             table.insert(connections, mouse.KeyUp:connect(function(key)
  244.                 local keyCode = key:byte()
  245.                 if (keyCode == 48) then
  246.                     shiftDown = false;
  247.                 end
  248.             end));
  249.         else
  250.             off()
  251.         end
  252.     end)
  253. end)
  254.  
  255. newPointButton.Click:connect(function()
  256.     spawn(function()
  257.         isPluginActive = not isPluginActive;
  258.        
  259.         if (isPluginActive) then
  260.             plugin:Activate(true);
  261.             isPluginActive = true;
  262.         end
  263.        
  264.         newPointButton_isActive = true;
  265.         newPointButton:SetActive(true);
  266.         if (isPluginActive) then
  267.            
  268.             if not (workspace:FindFirstChild("Plugin_Waypoints")) then
  269.                 folder = Instance.new("Folder", workspace)
  270.                 folder.Name = "Plugin_Waypoints"
  271.             else
  272.                 folder = workspace.Plugin_Waypoints;
  273.             end
  274.            
  275.             local mouse = plugin:GetMouse();
  276.             local currentPaintedTarget;
  277.            
  278.             table.insert(connections, mouse.Button1Down:connect(function()
  279.                 if (mouse.Target) then
  280.                     if (shiftDown == false) then
  281.                         generateWaypoint(mouse.Hit.p)
  282.                         saveProgress();
  283.                     else
  284.                         if (mouse.Target.Parent == folder and mouse.Target.Name == "Waypoint") then
  285.                             removeReferences(mouse.Target);
  286.                             mouse.Target:Destroy();
  287.                             saveProgress();
  288.                         end
  289.                     end
  290.                 end
  291.             end));
  292.            
  293.             table.insert(connections, mouse.Move:connect(function()
  294.                 if (currentPaintedTarget) then
  295.                     if (mouse.Target ~= currentPaintedTarget) then
  296.                         currentPaintedTarget.BrickColor = BrickColor.new("Really blue")
  297.                         currentPaintedTarget = nil;
  298.                     end
  299.                 else
  300.                     if (mouse.Target and shiftDown) then
  301.                         if (mouse.Target.Parent == folder and mouse.Target.Name == "Waypoint") then
  302.                             currentPaintedTarget = mouse.Target;
  303.                             currentPaintedTarget.BrickColor = BrickColor.new("Really red");
  304.                         end
  305.                     end
  306.                 end
  307.             end));
  308.            
  309.             table.insert(connections, mouse.KeyDown:connect(function(key)
  310.                 local keyCode = key:byte()
  311.                 if (keyCode == 48) then
  312.                     shiftDown = true;
  313.                 end
  314.             end));
  315.            
  316.             table.insert(connections, mouse.KeyUp:connect(function(key)
  317.                 local keyCode = key:byte()
  318.                 if (keyCode == 48) then
  319.                     shiftDown = false;
  320.                 end
  321.             end));
  322.         else
  323.             off()
  324.         end
  325.     end)
  326. end)
  327.  
  328. plugin.Deactivation:connect(function()
  329.     if (isPluginActive) then
  330.         off()
  331.     end
  332. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement