EWRWREWFF

Untitled

Jun 22nd, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. -- LocalScript placed in a Tool or Part
  2.  
  3. local UserInputService = game:GetService("UserInputService")
  4. local Players = game:GetService("Players")
  5. local Workspace = game:GetService("Workspace")
  6. local mouse = game.Players.LocalPlayer:GetMouse()
  7.  
  8. local isLocked = false
  9. local lockedPlayer = nil
  10.  
  11. local function findClosestPlayer()
  12. local closestPlayer = nil
  13. local shortestDistance = math.huge
  14. local mousePosition = mouse.Hit.p
  15.  
  16. for _, player in ipairs(game.Players:GetPlayers()) do
  17. local character = player.Character
  18. if character and character:FindFirstChild("HumanoidRootPart") then
  19. local distance = (character.HumanoidRootPart.Position - mousePosition).magnitude
  20. if distance < shortestDistance then
  21. shortestDistance = distance
  22. closestPlayer = player
  23. end
  24. end
  25. end
  26.  
  27. return closestPlayer
  28. end
  29.  
  30. UserInputService.InputBegan:Connect(function(input, isProcessed)
  31. if isProcessed then return end
  32. if input.KeyCode == Enum.KeyCode.E then
  33. isLocked = not isLocked
  34.  
  35. if isLocked then
  36. lockedPlayer = findClosestPlayer()
  37. if lockedPlayer then
  38. print("Locked onto:", lockedPlayer.Name)
  39. end
  40. else
  41. print("Lock released")
  42. lockedPlayer = nil
  43. end
  44. end
  45. end)
  46.  
  47. game:GetService("RunService").Heartbeat:Connect(function()
  48. if isLocked and lockedPlayer then
  49. -- Example: You might want to update the tool's position to follow lockedPlayer
  50. -- Replace this with your desired behavior (e.g., aiming the tool towards the lockedPlayer)
  51. -- For demonstration purposes, let's just print the distance.
  52. local distance = (lockedPlayer.Character.HumanoidRootPart.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
  53. print("Distance to locked player:", distance)
  54. end
  55. end)
  56.  
Advertisement
Add Comment
Please, Sign In to add comment