Flaviux

Untitled

Sep 9th, 2023
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. local player = game.Players.LocalPlayer
  2. local character = player.Character or player.CharacterAdded:Wait()
  3. local replicatedStorage = game:GetService("ReplicatedStorage")
  4. local runService = game:GetService("RunService")
  5. local parryButtonPress = replicatedStorage.Remotes.ParryButtonPress
  6. local ballsFolder = workspace:WaitForChild("Balls")
  7.  
  8. print("Script successfully ran.")
  9.  
  10. local function onCharacterAdded(newCharacter)
  11. character = newCharacter
  12. end
  13.  
  14. player.CharacterAdded:Connect(onCharacterAdded)
  15.  
  16. local focusedBall = nil
  17.  
  18. local function chooseNewFocusedBall()
  19. local balls = ballsFolder:GetChildren()
  20. focusedBall = nil
  21. for _, ball in ipairs(balls) do
  22. if ball:GetAttribute("realBall") == true then
  23. focusedBall = ball
  24. break
  25. end
  26. end
  27. end
  28.  
  29. chooseNewFocusedBall()
  30.  
  31. local function timeUntilImpact(ballVelocity, distanceToPlayer, playerVelocity)
  32. local directionToPlayer = (character.HumanoidRootPart.Position - focusedBall.Position).Unit
  33. local velocityTowardsPlayer = ballVelocity:Dot(directionToPlayer) - playerVelocity:Dot(directionToPlayer)
  34.  
  35. if velocityTowardsPlayer <= 0 then
  36. return math.huge
  37. end
  38.  
  39. local distanceToBeCovered = distanceToPlayer - 40
  40. return distanceToBeCovered / velocityTowardsPlayer
  41. end
  42.  
  43. local BASE_THRESHOLD = 0.15
  44. local VELOCITY_SCALING_FACTOR = 0.002
  45.  
  46. local function getDynamicThreshold(ballVelocityMagnitude)
  47. local adjustedThreshold = BASE_THRESHOLD - (ballVelocityMagnitude * VELOCITY_SCALING_FACTOR)
  48. return math.max(0.12, adjustedThreshold)
  49. end
  50.  
  51. local function checkBallDistance()
  52. if not character:FindFirstChild("Highlight") then return end
  53. local charPos = character.PrimaryPart.Position
  54. local charVel = character.PrimaryPart.Velocity
  55.  
  56. if focusedBall and not focusedBall.Parent then
  57. chooseNewFocusedBall()
  58. end
  59.  
  60. if not focusedBall then return end
  61.  
  62. local ball = focusedBall
  63. local distanceToPlayer = (ball.Position - charPos).Magnitude
  64.  
  65. if distanceToPlayer < 10 then
  66. parryButtonPress:Fire()
  67. return
  68. end
  69.  
  70. local timeToImpact = timeUntilImpact(ball.Velocity, distanceToPlayer, charVel)
  71. local dynamicThreshold = getDynamicThreshold(ball.Velocity.Magnitude)
  72.  
  73. if timeToImpact < dynamicThreshold then
  74. parryButtonPress:Fire()
  75. end
  76. end
  77.  
  78.  
  79.  
  80. runService.Heartbeat:Connect(function()
  81. checkBallDistance()
  82. end)
Add Comment
Please, Sign In to add comment