Advertisement
SigmaBoy456

Roblox Bring Each Player( Check tool Only)

Aug 11th, 2024
2,093
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. --CREDIT:
  2. -- Script Written and Edited by MawinCK
  3. -- Script Fixed and Edited by ChatGPT
  4. local player = game.Players.LocalPlayer
  5. local character = player.Character or player.CharacterAdded:Wait()
  6. local localroot = character:WaitForChild("HumanoidRootPart")
  7.  
  8. local RunService = game:GetService("RunService")
  9. local connection = nil
  10.  
  11. local currentTarget = nil -- Track the current target player
  12.  
  13. local function UpdateTargetPlayers()
  14. local newTarget = nil
  15. for _, v in pairs(game.Players:GetPlayers()) do
  16. if v ~= player and v.Character then
  17. local JNR = v.Character:FindFirstChild("Humanoid")
  18. if JNR and JNR.Health > 0 and (v.Character:FindFirstChildOfClass("Tool") or v.Backpack:FindFirstChildOfClass("Tool")) then
  19. newTarget = v
  20. break -- Take the first valid target
  21. end
  22. end
  23. end
  24. return newTarget
  25. end
  26.  
  27. local function Disconnect()
  28. if connection then
  29. connection:Disconnect()
  30. connection = nil
  31. end
  32. end
  33.  
  34. local function BringTargetPlayerUntilHealthZero()
  35. Disconnect() -- Ensure any previous connections are cleared
  36. connection = RunService.RenderStepped:Connect(function()
  37. if currentTarget then
  38. local JN = currentTarget.Character:FindFirstChild("HumanoidRootPart")
  39. local JNR = currentTarget.Character:FindFirstChild("Humanoid")
  40. if JN and JNR and JNR.Health > 0 then
  41. JN.CFrame = localroot.CFrame * CFrame.new(0, 0, -3)
  42. else
  43. -- If current target's health is 0 or no longer valid, find a new target
  44. currentTarget = UpdateTargetPlayers()
  45. end
  46. else
  47. -- Find a new target if there is no current one
  48. currentTarget = UpdateTargetPlayers()
  49. end
  50. end)
  51. end
  52.  
  53. local function Reinitialize()
  54. Disconnect() -- Ensure any previous connections are cleared
  55. currentTarget = UpdateTargetPlayers()
  56. BringTargetPlayerUntilHealthZero()
  57. end
  58.  
  59. -- Initialize the targeting process
  60. Reinitialize()
  61.  
  62. player.CharacterAdded:Connect(function(char)
  63. character = char
  64. localroot = character:WaitForChild("HumanoidRootPart")
  65. -- Reinitialize the targeting process
  66. Reinitialize()
  67. end)
  68.  
  69. game.Players.PlayerAdded:Connect(Reinitialize)
  70.  
  71. game.Players.PlayerRemoving:Connect(Reinitialize)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement