Advertisement
julialy

a better drag clone tool

Aug 4th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. local Tool = script.Parent
  2.  
  3. local selectionBox
  4. local currentSelection
  5. local dragger
  6. local lockTime = 0
  7. local cloneTool = true
  8. local cloneSound
  9. local antiAbuse = true
  10. local antiAbuseTime = 0.5
  11. local isE = false
  12. local mouse
  13. local debounce = false
  14.  
  15. local ev = {}
  16.  
  17.  
  18. function canSelectObject(part)
  19. return part and part:IsA("BasePart") and (not (part.Locked))
  20. end
  21.  
  22. function startDrag(mousePart, hitPoint, collection)
  23. selectionBox.Adornee = mousePart
  24. dragger = Instance.new("Dragger")
  25. pcall(function() dragger:MouseDown(mousePart, hitPoint, collection) end)
  26. end
  27.  
  28. function onMouseDown(mouse)
  29. local part = mouse.Target
  30. if canSelectObject(part) then
  31. local hitPoint = part.CFrame:toObjectSpace(CFrame.new(mouse.Hit.p)).p
  32. if trySelection(part) then
  33. if cloneTool then
  34. if debounce == false then
  35. debounce = true
  36. local cloned = part:clone()
  37.  
  38. cloned.Parent = game.Workspace
  39. cloned.Position = part.Position + Vector3.new(0, 0.4, 0)
  40.  
  41. cloneSound:Play()
  42. Spawn(function()
  43. if antiAbuse then
  44. wait(antiAbuseTime)
  45. end
  46. debounce = false
  47. end)
  48. if trySelection(cloned) then
  49. mouse.Icon = "rbxasset://textures/DragCursor.png"
  50. startDrag(cloned, hitPoint, {cloned})
  51. return
  52. end
  53. end
  54. else --dragtool
  55. mouse.Icon = "rbxasset://textures/DragCursor.png"
  56. startDrag(part, hitPoint, {part})
  57. return
  58. end
  59.  
  60. end
  61. end
  62.  
  63. --Clear the selection if we weren't able to lock succesfullu
  64. onMouseUp(mouse)
  65. end
  66.  
  67.  
  68.  
  69. function onMouseUp(mouse)
  70. if dragger ~= nil then
  71. pcall(function() dragger:MouseUp() end)
  72. dragger = nil
  73. clearSelection()
  74. onMouseMove(mouse)
  75. end
  76. end
  77.  
  78. function trySelection(part)
  79. if canSelectObject(part) then
  80. return setSelection(part)
  81. else
  82. clearSelection()
  83. return false
  84. end
  85. end
  86.  
  87. function onKeyDown(key)
  88. if dragger ~= nil then
  89. if key == 'R' or key == 'r' then
  90. dragger:AxisRotate(Enum.Axis.Y)
  91. elseif key == 'T' or key == 't' then
  92. dragger:AxisRotate(Enum.Axis.Z)
  93. end
  94. end
  95. end
  96. local alreadyMoving
  97. function onMouseMove(mouse)
  98. if alreadyMoving then
  99. return
  100. end
  101.  
  102. alreadyMoving = true
  103. if dragger ~= nil then
  104. mouse.Icon = "rbxasset://textures/GrabRotateCursor.png"
  105. --Maintain the lock
  106. if time() - lockTime > 3 then
  107. Instance.Lock(currentSelection)
  108. lockTime = time()
  109. end
  110.  
  111. --Then drag
  112. pcall(function() dragger:MouseMove(mouse.UnitRay) end)
  113. else
  114. if trySelection(mouse.Target) then
  115. mouse.Icon = (cloneTool and "rbxasset://textures/CloneOverCursor.png") or "rbxasset://textures/DragCursor.png"
  116. else
  117. mouse.Icon = (cloneTool and "rbxasset://textures/CloneCursor.png") or "rbxasset://textures/ArrowFarCursor.png"
  118. end
  119. end
  120. alreadyMoving = false
  121. end
  122.  
  123.  
  124. function setSelection(partOrModel)
  125. if partOrModel ~= currentSelection then
  126. clearSelection()
  127. if Instance.Lock(partOrModel) then
  128. lockTime = time()
  129. currentSelection = partOrModel
  130.  
  131. return true
  132. end
  133. else
  134. if currentSelection ~= nil then
  135. if time() - lockTime > 2 then
  136. --Maintain the lock
  137. if not(Instance.Lock(currentSelection)) then
  138. --we lost the lock
  139. clearSelection()
  140. return false
  141. else
  142. lockTime = time()
  143. return true
  144. end
  145. else
  146. return true
  147. end
  148. end
  149. end
  150.  
  151. return false
  152. end
  153.  
  154. function clearSelection()
  155. if currentSelection ~= nil then
  156. Instance.Unlock(currentSelection)
  157. end
  158. currentSelection = nil
  159. selectionBox.Adornee = nil
  160. end
  161.  
  162. function onEquippedLocal()
  163. if not isE then --roblox bugged tools somehow
  164. isE = true
  165. else
  166. return
  167. end
  168.  
  169. local player = game.Players.LocalPlayer
  170. local character = player.Character
  171. mouse = player:GetMouse()
  172.  
  173. table.insert(ev, mouse.Button1Down:connect(function() onMouseDown(mouse) end))
  174. table.insert(ev, mouse.Button1Up:connect(function() onMouseUp(mouse) end))
  175. table.insert(ev, mouse.Move:connect(function() onMouseMove(mouse) end))
  176. table.insert(ev, mouse.KeyDown:connect(function(string) onKeyDown(string) end))
  177.  
  178. cloneSound = Instance.new("Sound")
  179. cloneSound.SoundId = "rbxasset://sounds/electronicpingshort.wav"
  180. cloneSound.Parent = Tool
  181.  
  182. selectionBox = Instance.new("SelectionBox")
  183. selectionBox.Name = "Model Delete Selection"
  184. selectionBox.Color3 = Color3.new(25/255, 153/255, 255/255)
  185. selectionBox.LineThickness = 0.02
  186. selectionBox.Adornee = nil
  187. selectionBox.Parent = player.PlayerGui
  188.  
  189. alreadyMoving = false
  190. Spawn(function()
  191. wait()
  192. onMouseMove(mouse)
  193. end)
  194. end
  195.  
  196. function onUnequippedLocal()
  197. isE = false
  198. for i, v in pairs(ev) do
  199. v:disconnect()
  200. end
  201. mouse.Icon = ""
  202. ev = {}
  203. clearSelection()
  204. selectionBox:Remove()
  205. cloneSound:Remove()
  206. end
  207.  
  208.  
  209. Tool.Equipped:connect(onEquippedLocal)
  210. Tool.Unequipped:connect(onUnequippedLocal)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement