Kenken_I

Open source reach

May 6th, 2025
1,932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 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 = 15
  8. local MIN_REACH = 9.1
  9. local DETECTION_RANGE = 9.01
  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. local warningText
  26.  
  27. --// GUI: Display the warning message and reach adjustment
  28. local function createWarningText()
  29. local screenGui = Instance.new("ScreenGui")
  30. screenGui.Name = "PingWarningGUI"
  31. screenGui.Parent = LocalPlayer.PlayerGui
  32.  
  33. warningText = Instance.new("TextLabel")
  34. warningText.Size = UDim2.new(0, 300, 0, 50)
  35. warningText.Position = UDim2.new(0.5, -150, 0.9, -25)
  36. warningText.Text = ""
  37. warningText.TextColor3 = Color3.fromRGB(255, 0, 0)
  38. warningText.BackgroundTransparency = 1
  39. warningText.TextSize = 20
  40. warningText.Parent = screenGui
  41. end
  42.  
  43. --// Create visual bubble
  44. local function createBubble()
  45. local part = Instance.new("Part")
  46. part.Name = "SafeReachBubble"
  47. part.Anchored = true
  48. part.CanCollide = false
  49. part.CastShadow = false
  50. part.Shape = Enum.PartType.Ball
  51. part.Material = Enum.Material.ForceField
  52. part.Transparency = 0.2
  53. part.Color = Color3.fromHSV(0, 1, 1)
  54. part.Size = Vector3.new(MAX_REACH * 2, MAX_REACH * 2, MAX_REACH * 2)
  55. part.Parent = LocalPlayer.Character
  56. return part
  57. end
  58.  
  59. -- Raycast check to see if player is visible
  60. local function isClear(origin, targetPos, targetChar)
  61. local dir = targetPos - origin
  62. local params = RaycastParams.new()
  63. params.FilterType = Enum.RaycastFilterType.Whitelist
  64. params.FilterDescendantsInstances = { targetChar }
  65. params.IgnoreWater = true
  66.  
  67. local result = workspace:Raycast(origin, dir, params)
  68. return result and result.Instance and result.Instance:IsDescendantOf(targetChar)
  69. end
  70.  
  71. -- Simulate touch
  72. local function touchCharacter(char, fromPos)
  73. local hrp = char:FindFirstChild("HumanoidRootPart")
  74. local handle = tool and tool:FindFirstChild("Handle")
  75. if not handle or not hrp then return end
  76.  
  77. local distance = (fromPos - hrp.Position).Magnitude
  78. if distance > MAX_REACH then return end
  79.  
  80. for _, part in ipairs(char:GetChildren()) do
  81. if part:IsA("BasePart") then
  82. task.delay(FIRETOUCH_DELAY, function()
  83. firetouchinterest(handle, part, 0)
  84. firetouchinterest(handle, part, 1)
  85. end)
  86. end
  87. end
  88. end
  89.  
  90. -- Check ping and adjust reach if necessary
  91. local function checkPingAndAdjustReach(target)
  92. local ping = target.GetNetworkPing and target:GetNetworkPing()
  93. if ping and ping > 230 then
  94. MAX_REACH = 7.1
  95. CLOSE_HIT = 8.4
  96. DETECTION_RANGE = 7.5
  97. warningText.Text = "Warning: Ping is over 230ms! Reach reduced."
  98. else
  99. MAX_REACH = 20
  100. CLOSE_HIT = 9
  101. DETECTION_RANGE = 8
  102. warningText.Text = ""
  103. end
  104. end
  105.  
  106. -- Determine reach based on movement
  107. local function getAdjustedReach(player, hrp, humanoid)
  108. if JUMP_STATES[humanoid:GetState()] then
  109. return MIN_REACH
  110. end
  111.  
  112. local moveDir = humanoid.MoveDirection
  113. local toPlayer = (LocalPlayer.Character.HumanoidRootPart.Position - hrp.Position).Unit
  114.  
  115. if moveDir.Magnitude > 0.1 and moveDir:Dot(-toPlayer) > 0.5 then
  116. return MIN_REACH
  117. end
  118.  
  119. return MAX_REACH
  120. end
  121.  
  122. -- Stop the update loop
  123. local function stopLoop()
  124. if heartbeatConnection then
  125. heartbeatConnection:Disconnect()
  126. heartbeatConnection = nil
  127. end
  128. end
  129.  
  130. -- Main logic when tool is equipped
  131. local function onEquipped()
  132. equipped = true
  133. tool = LocalPlayer.Character and LocalPlayer.Character:FindFirstChildOfClass("Tool")
  134. if not tool then return end
  135.  
  136. if not bubble then
  137. bubble = createBubble()
  138. end
  139.  
  140. stopLoop()
  141.  
  142. local lastUpdate = 0
  143. heartbeatConnection = RunService.Heartbeat:Connect(function()
  144. local currentTime = tick()
  145. if currentTime - lastUpdate < UPDATE_RATE then return end
  146. lastUpdate = currentTime
  147.  
  148. local char = LocalPlayer.Character
  149. local hrp = char and char:FindFirstChild("HumanoidRootPart")
  150. if not equipped or not tool or not hrp then return end
  151.  
  152. -- Animate visual bubble
  153. hue = (hue + 0.01) % 1
  154. if bubble then
  155. bubble.Position = hrp.Position
  156. bubble.Color = Color3.fromHSV(hue, 1, 1)
  157. end
  158.  
  159. for _, target in ipairs(Players:GetPlayers()) do
  160. if target ~= LocalPlayer and target.Character then
  161. local tChar = target.Character
  162. local tHRP = tChar:FindFirstChild("HumanoidRootPart")
  163. local humanoid = tChar:FindFirstChildOfClass("Humanoid")
  164.  
  165. if tHRP and humanoid and humanoid.Health > 0 then
  166. checkPingAndAdjustReach(target)
  167.  
  168. local rawDistance = (hrp.Position - tHRP.Position).Magnitude
  169. if rawDistance > DETECTION_RANGE then continue end
  170.  
  171. local reach = getAdjustedReach(target, tHRP, humanoid)
  172. if bubble then
  173. bubble.Size = Vector3.new(reach * 2, reach * 2, reach * 2)
  174. end
  175.  
  176. if rawDistance <= reach then
  177. if rawDistance <= CLOSE_HIT then
  178. touchCharacter(tChar, hrp.Position)
  179. else
  180. local predicted = tHRP.Position + tHRP.Velocity * PREDICTION
  181. if isClear(hrp.Position, predicted, tChar) or isClear(hrp.Position, tHRP.Position, tChar) then
  182. touchCharacter(tChar, hrp.Position)
  183. end
  184. end
  185. end
  186. end
  187. end
  188. end
  189. end)
  190. end
  191.  
  192. -- Logic when tool is unequipped
  193. local function onUnequipped()
  194. equipped = false
  195. stopLoop()
  196. if bubble then
  197. bubble:Destroy()
  198. bubble = nil
  199. end
  200. end
  201.  
  202. -- Setup tool listeners
  203. local function setupCharacter(char)
  204. local humanoid = char:FindFirstChildOfClass("Humanoid")
  205. local backpack = LocalPlayer:FindFirstChild("Backpack")
  206. if humanoid and backpack then
  207. local toolInBackpack = backpack:FindFirstChildOfClass("Tool")
  208. if toolInBackpack then
  209. humanoid:EquipTool(toolInBackpack)
  210. end
  211. end
  212.  
  213. char.ChildAdded:Connect(function(child)
  214. if child:IsA("Tool") then
  215. child.Equipped:Connect(onEquipped)
  216. child.Unequipped:Connect(onUnequipped)
  217. end
  218. end)
  219.  
  220. for _, child in ipairs(char:GetChildren()) do
  221. if child:IsA("Tool") then
  222. child.Equipped:Connect(onEquipped)
  223. child.Unequipped:Connect(onUnequipped)
  224. end
  225. end
  226. end
  227.  
  228. -- INIT
  229. if LocalPlayer.Character then
  230. setupCharacter(LocalPlayer.Character)
  231. createWarningText() -- Create the warning text GUI on init
  232. end
  233.  
  234. LocalPlayer.CharacterAdded:Connect(function()
  235. setupCharacter(LocalPlayer.Character)
  236. end)
Advertisement
Add Comment
Please, Sign In to add comment