Kenken_I

Untitled

May 3rd, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. --// SERVICES
  2. local Players = game:GetService("Players")
  3. local RunService = game:GetService("RunService")
  4. local LocalPlayer = Players.LocalPlayer
  5.  
  6. --// SETTINGS
  7. local MAX_REACH = 14
  8. local MIN_REACH = 14
  9. local DETECTION_RANGE = 21
  10. local CLOSE_HIT = 9
  11. local PREDICTION = 0.18
  12. local FIRETOUCH_DELAY = 0
  13. local UPDATE_RATE = 1 / 60 -- 60 updates per second
  14.  
  15. local JUMP_STATES = {
  16. [Enum.HumanoidStateType.Jumping] = true,
  17. [Enum.HumanoidStateType.Freefall] = true
  18. }
  19.  
  20. --// VARIABLES
  21. local equipped = false
  22. local tool, bubble = nil, nil
  23. local hue = 0
  24. local heartbeatConnection
  25.  
  26. --// FUNCTIONS
  27.  
  28. -- Create visual bubble
  29. local function createBubble()
  30. local part = Instance.new("Part")
  31. part.Name = "SafeReachBubble"
  32. part.Anchored = true
  33. part.CanCollide = false
  34. part.CastShadow = false
  35. part.Shape = Enum.PartType.Ball
  36. part.Material = Enum.Material.ForceField
  37. part.Transparency = 0.2
  38. part.Color = Color3.fromHSV(0, 1, 1)
  39. part.Size = Vector3.new(MAX_REACH * 2, MAX_REACH * 2, MAX_REACH * 2)
  40. part.Parent = LocalPlayer.Character
  41. return part
  42. end
  43.  
  44. -- Raycast check to see if player is visible
  45. local function isClear(origin, targetPos, targetChar)
  46. local dir = targetPos - origin
  47. local params = RaycastParams.new()
  48. params.FilterType = Enum.RaycastFilterType.Whitelist
  49. params.FilterDescendantsInstances = { targetChar }
  50. params.IgnoreWater = true
  51.  
  52. local result = workspace:Raycast(origin, dir, params)
  53. return result and result.Instance and result.Instance:IsDescendantOf(targetChar)
  54. end
  55.  
  56. -- Simulate touch
  57. local function touchCharacter(char, fromPos)
  58. local hrp = char:FindFirstChild("HumanoidRootPart")
  59. local handle = tool and tool:FindFirstChild("Handle")
  60. if not handle or not hrp then return end
  61.  
  62. local distance = (fromPos - hrp.Position).Magnitude
  63. if distance > MAX_REACH then return end
  64.  
  65. for _, part in ipairs(char:GetChildren()) do
  66. if part:IsA("BasePart") then
  67. task.delay(FIRETOUCH_DELAY, function()
  68. firetouchinterest(handle, part, 0)
  69. firetouchinterest(handle, part, 1)
  70. end)
  71. end
  72. end
  73. end
  74.  
  75. -- Determine reach based on movement
  76. local function getAdjustedReach(player, hrp, humanoid)
  77. if JUMP_STATES[humanoid:GetState()] then
  78. return MIN_REACH
  79. end
  80.  
  81. local moveDir = humanoid.MoveDirection
  82. local toPlayer = (LocalPlayer.Character.HumanoidRootPart.Position - hrp.Position).Unit
  83.  
  84. if moveDir.Magnitude > 0.1 and moveDir:Dot(-toPlayer) > 0.5 then
  85. return MIN_REACH
  86. end
  87.  
  88. return MAX_REACH
  89. end
  90.  
  91. -- Stop the update loop
  92. local function stopLoop()
  93. if heartbeatConnection then
  94. heartbeatConnection:Disconnect()
  95. heartbeatConnection = nil
  96. end
  97. end
  98.  
  99. -- Main logic when tool is equipped
  100. local function onEquipped()
  101. equipped = true
  102. tool = LocalPlayer.Character and LocalPlayer.Character:FindFirstChildOfClass("Tool")
  103. if not tool then return end
  104.  
  105. if not bubble then
  106. bubble = createBubble()
  107. end
  108.  
  109. stopLoop()
  110.  
  111. local lastUpdate = 0
  112. heartbeatConnection = RunService.Heartbeat:Connect(function()
  113. local currentTime = tick()
  114. if currentTime - lastUpdate < UPDATE_RATE then return end
  115. lastUpdate = currentTime
  116.  
  117. local char = LocalPlayer.Character
  118. local hrp = char and char:FindFirstChild("HumanoidRootPart")
  119. if not equipped or not tool or not hrp then return end
  120.  
  121. -- Animate visual bubble
  122. hue = (hue + 0.01) % 1
  123. if bubble then
  124. bubble.Position = hrp.Position
  125. bubble.Color = Color3.fromHSV(hue, 1, 1)
  126. end
  127.  
  128. for _, target in ipairs(Players:GetPlayers()) do
  129. if target ~= LocalPlayer and target.Character then
  130. local tChar = target.Character
  131. local tHRP = tChar:FindFirstChild("HumanoidRootPart")
  132. local humanoid = tChar:FindFirstChildOfClass("Humanoid")
  133.  
  134. if tHRP and humanoid and humanoid.Health > 0 then
  135. local rawDistance = (hrp.Position - tHRP.Position).Magnitude
  136. if rawDistance > DETECTION_RANGE then continue end
  137.  
  138. local reach = getAdjustedReach(target, tHRP, humanoid)
  139. if bubble then
  140. bubble.Size = Vector3.new(reach * 2, reach * 2, reach * 2)
  141. end
  142.  
  143. if rawDistance <= reach then
  144. if rawDistance <= CLOSE_HIT then
  145. touchCharacter(tChar, hrp.Position)
  146. else
  147. local predicted = tHRP.Position + tHRP.Velocity * PREDICTION
  148. if isClear(hrp.Position, predicted, tChar) or isClear(hrp.Position, tHRP.Position, tChar) then
  149. touchCharacter(tChar, hrp.Position)
  150. end
  151. end
  152. end
  153. end
  154. end
  155. end
  156. end)
  157. end
  158.  
  159. -- Logic when tool is unequipped
  160. local function onUnequipped()
  161. equipped = false
  162. stopLoop()
  163. if bubble then
  164. bubble:Destroy()
  165. bubble = nil
  166. end
  167. end
  168.  
  169. -- Setup tool listeners
  170. local function setupCharacter(char)
  171. local humanoid = char:FindFirstChildOfClass("Humanoid")
  172. local backpack = LocalPlayer:FindFirstChild("Backpack")
  173. if humanoid and backpack then
  174. local toolInBackpack = backpack:FindFirstChildOfClass("Tool")
  175. if toolInBackpack then
  176. humanoid:EquipTool(toolInBackpack)
  177. end
  178. end
  179.  
  180. char.ChildAdded:Connect(function(child)
  181. if child:IsA("Tool") then
  182. child.Equipped:Connect(onEquipped)
  183. child.Unequipped:Connect(onUnequipped)
  184. end
  185. end)
  186.  
  187. for _, child in ipairs(char:GetChildren()) do
  188. if child:IsA("Tool") then
  189. child.Equipped:Connect(onEquipped)
  190. child.Unequipped:Connect(onUnequipped)
  191. end
  192. end
  193. end
  194.  
  195. -- INIT
  196. if LocalPlayer.Character then
  197. setupCharacter(LocalPlayer.Character)
  198. end
  199.  
  200. LocalPlayer.CharacterAdded:Connect(setupCharacter)
Advertisement
Add Comment
Please, Sign In to add comment