Advertisement
SigmaBoy456

Roblox Bring Each Player until they died script(Distance: 3)

Aug 11th, 2024 (edited)
2,233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 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 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. -- Initialize the targeting process
  54. currentTarget = UpdateTargetPlayers()
  55. BringTargetPlayerUntilHealthZero()
  56.  
  57. player.CharacterAdded:Connect(function(char)
  58. character = char
  59. localroot = character:WaitForChild("HumanoidRootPart")
  60. -- Reinitialize the targeting process
  61. currentTarget = UpdateTargetPlayers()
  62. BringTargetPlayerUntilHealthZero()
  63. end)
  64.  
  65. game.Players.PlayerAdded:Connect(function()
  66. -- Reinitialize the targeting process
  67. currentTarget = UpdateTargetPlayers()
  68. BringTargetPlayerUntilHealthZero()
  69. end)
  70.  
  71. game.Players.PlayerRemoving:Connect(function()
  72. -- Reinitialize the targeting process
  73. currentTarget = UpdateTargetPlayers()
  74. BringTargetPlayerUntilHealthZero()
  75. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement