LePooti

Untitled

Jul 21st, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. function waitForChild(parent, childName)
  2. while true do
  3. local child = parent:FindFirstChild(childName)
  4. if child then
  5. return child
  6. end
  7. parent.ChildAdded:wait()
  8. end
  9. end
  10.  
  11. print("Loading telekinesis script...")
  12. bin = script.Parent
  13. player = bin.Parent.Parent
  14. damage = waitForChild(bin, "BrickScript")
  15.  
  16. local debounce = false
  17.  
  18. local TKCF = nil
  19.  
  20. local grValue = Instance.new("NumberValue")
  21. grValue.Value = 10.5
  22. grValue.Name = "Grab Radius"
  23. grValue.archivable = false
  24. grValue.Parent = bin
  25.  
  26. local tkrValue = Instance.new("NumberValue")
  27. tkrValue.Value = 32
  28. tkrValue.Name = "Telekinesis Radius"
  29. tkrValue.archivable = false
  30. tkrValue.Parent = bin
  31.  
  32. local rldValue = Instance.new("NumberValue")
  33. rldValue.Value = 1
  34. rldValue.Name = "Reload Time"
  35. rldValue.archivable = false
  36. rldValue.Parent = bin
  37.  
  38. local maxFValue = Instance.new("NumberValue")
  39. maxFValue.Value = 1100400
  40. maxFValue.Name = "Max Force"
  41. maxFValue.archivable = false
  42. maxFValue.Parent = bin
  43.  
  44. --TKRadius = 32
  45.  
  46. TKPos = Vector3.new(0, 0, 0)
  47.  
  48. local totalMass = 0
  49.  
  50. function brickDistance(obj, pos)
  51. if (obj.Shape == 0) then
  52. return ((pos - obj.Position).magnitude - obj.Size.magnitude / 2)
  53. else
  54. local relPos = obj.CFrame:pointToObjectSpace(pos)
  55. local nearPos = relPos
  56. if (nearPos.x * 2 > obj.Size.x) then
  57. nearPos = Vector3.new(obj.Size.x / 2, nearPos.y, nearPos.z)
  58. elseif (nearPos.x * 2 < -obj.Size.x) then
  59. nearPos = Vector3.new(-obj.Size.x / 2, nearPos.y, nearPos.z)
  60. end
  61. if (nearPos.y * 2 > obj.Size.y) then
  62. nearPos = Vector3.new(nearPos.x, obj.Size.y / 2, nearPos.z)
  63. elseif (nearPos.y * 2 < -obj.Size.y) then
  64. nearPos = Vector3.new(nearPos.x, -obj.Size.y / 2, nearPos.z)
  65. end
  66. if (nearPos.z * 2 > obj.Size.z) then
  67. nearPos = Vector3.new(nearPos.x, nearPos.y, obj.Size.x / 2)
  68. elseif (nearPos.z * 2 < -obj.Size.z) then
  69. nearPos = Vector3.new(nearPos.x, nearPos.y, -obj.Size.x / 2)
  70. end
  71. return (nearPos - relPos).magnitude
  72. end
  73. end
  74.  
  75. function drop()
  76. if TKCF ~= nil then
  77. print("Telekinesis: Dropping parts...")
  78. TKCF:Remove()
  79. TKCF = nil
  80. end
  81. end
  82.  
  83. function grab(pos, obj, harmless, MaxForce)
  84. if (obj:FindFirstChild("Telekinesis Influence") ~= nil) then -- Prevent muliple grabs
  85. return
  86. end
  87. if (obj.className == "Part") or (obj.className == "Seat") or (obj.className == "SpawnLocation") then
  88. if (brickDistance(obj, pos) < grValue.Value) and (not obj.Anchored) and ((h == nil) or (h.Health <= 0)) then
  89. local harm = obj:FindFirstChild("No TK Damage")
  90. local oldScript = obj:FindFirstChild("BrickScript")
  91. if oldScript ~= nil then
  92. oldScript:Remove()
  93. end
  94. -- When possible, make objects be broken off from others. (But not from other parts that are grabbed)
  95. --obj:BreakJoints()
  96.  
  97. --local tag = Instance.new("ObjectValue")
  98. --tag.Name = "creator"
  99. --tag.Value = player
  100. --tag.Parent = obj
  101.  
  102. local damageCopy = damage:Clone()
  103. damageCopy.Disabled = false
  104. damageCopy.archivable = false
  105.  
  106. if harmless or (harm ~= nil) then
  107. local noDamage = Instance.new("BoolValue")
  108. noDamage.Name = "No Damage"
  109. noDamage.Parent = damageCopy
  110. end
  111.  
  112. local tag2 = Instance.new("ObjectValue")
  113. tag2.Name = "TK CFrame"
  114. tag2.Value = TKCF
  115. tag2.Parent = damageCopy
  116.  
  117. damageCopy.Parent = obj
  118. -- Now adjust brick force to be within MaxForce
  119. local mass = obj:GetMass()
  120. totalMass = totalMass + mass
  121. delay(0.1, function() if (totalMass * 1400 > MaxForce) and (mass * MaxForce / totalMass < 91700) then local bp = obj:FindFirstChild("Telekinesis Influence") if (bp ~= nil) then bp.maxForce = mass * Vector3.new(1, 1, 1) * MaxForce / totalMass end end end)
  122. end
  123. elseif ((obj.className == "Workspace") or (obj.className == "Model") or (obj.className == "Tool") or (obj.className == "Hat")) and (obj ~= player.Character) then
  124. local h = obj:FindFirstChild("Humanoid")
  125. local harm = obj:FindFirstChild("No TK Damage")
  126. if (h == nil) or (h.Health <= 0) then
  127. local list = obj:GetChildren()
  128. for x = 1, #list do
  129. grab(pos, list[x], (harm ~= nil) or harmless, MaxForce)
  130. end
  131. end
  132. end
  133. end
  134.  
  135.  
  136. function onButton1Down(mouse)
  137. if debounce then
  138. return
  139. end
  140. local newDamage = bin:FindFirstChild("BrickScript")
  141. if newDamage ~= nil then
  142. damage = newDamage
  143. end
  144. drop()
  145. local char = player.Character
  146. if char ~= nil then
  147. local human = char.Humanoid
  148. if (human == nil) or (human.Health <= 0) then
  149. return
  150. end
  151. else
  152. return
  153. end
  154. TKCF = Instance.new("CFrameValue")
  155. TKCF.Name = "TK CFrame"
  156. TKCF.Parent = bin
  157.  
  158. --if (mouse.Target ~= nil) then
  159. local relPos = mouse.Hit.p - player.Character.PrimaryPart.Position
  160. if (relPos.magnitude > tkrValue.Value) then
  161. relPos = relPos.unit * tkrValue.Value
  162. end
  163. totalMass = 0
  164. print("Telekinesis: Grabbing parts...")
  165. grab(player.Character.PrimaryPart.Position + relPos, workspace, false, maxFValue.Value)
  166. --end
  167. mouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
  168. end
  169.  
  170. function onButton1Up(mouse)
  171. if (TKCF ~= nil) then
  172. drop()
  173. debounce = true
  174. wait(rldValue.Value)
  175. mouse.Icon = "rbxasset://textures\\GunCursor.png"
  176. debounce = false
  177. end
  178. end
  179.  
  180. function updatePos(mouse)
  181. if (TKCF ~= nil) then
  182. local char = player.Character
  183. if char ~= nil then
  184. local human = char.Humanoid
  185. if (human == nil) or (human.Health <= 0) then
  186. drop()
  187. return
  188. end
  189. else
  190. drop()
  191. return
  192. end
  193. mouseCF = mouse.Hit
  194. TKPos = mouseCF.lookVector * tkrValue.Value
  195. if TKPos.y < -4.5 then
  196. TKPos = TKPos * Vector3.new(1, 0, 1) + Vector3.new(0, -4.5, 0)
  197. end
  198. TKPos = TKPos + player.Character.PrimaryPart.Position
  199. TKCF.Value = CFrame.new(TKPos, TKPos + mouseCF.lookVector)
  200. end
  201. end
  202.  
  203. function onSelected(mouse)
  204. print("Telekinesis selected")
  205. mouse.Icon = "rbxasset://textures\\GunCursor.png"
  206. mouseConnection = mouse.Button1Down:connect(function() onButton1Down(mouse) end)
  207. mouse.Button1Up:connect(function() onButton1Up(mouse) end)
  208. mouse.Idle:connect(function() updatePos(mouse) end)
  209. mouse.Move:connect(function() updatePos(mouse) end)
  210. end
  211.  
  212. function onDeselected()
  213. if (mouseConnection ~= nil) then
  214. mouseConnection:disconnect()
  215. end
  216. drop()
  217. end
  218.  
  219. selectionConnection = bin.Selected:connect(onSelected)
  220. bin.Deselected:connect(onDeselected)
Add Comment
Please, Sign In to add comment