Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --CREDIT:
- -- Script Written and Edited by MawinCK
- -- Script Fixed and Edited by ChatGPT
- local player = game.Players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local localroot = character:WaitForChild("HumanoidRootPart")
- local RunService = game:GetService("RunService")
- local connection = nil
- local currentTarget = nil -- Track the current target player
- local function UpdateTargetPlayers()
- local newTarget = nil
- for _, v in pairs(game.Players:GetPlayers()) do
- if v ~= player and v.Character then
- local JNR = v.Character:FindFirstChild("Humanoid")
- if JNR and JNR.Health > 0 and (v.Character:FindFirstChildOfClass("Tool") or v.Backpack:FindFirstChildOfClass("Tool")) then
- newTarget = v
- break -- Take the first valid target
- end
- end
- end
- return newTarget
- end
- local function Disconnect()
- if connection then
- connection:Disconnect()
- connection = nil
- end
- end
- local function BringTargetPlayerUntilHealthZero()
- Disconnect() -- Ensure any previous connections are cleared
- connection = RunService.RenderStepped:Connect(function()
- if currentTarget then
- local JN = currentTarget.Character:FindFirstChild("HumanoidRootPart")
- local JNR = currentTarget.Character:FindFirstChild("Humanoid")
- if JN and JNR and JNR.Health > 0 then
- JN.CFrame = localroot.CFrame * CFrame.new(0, 0, -3)
- else
- -- If current target's health is 0 or no longer valid, find a new target
- currentTarget = UpdateTargetPlayers()
- end
- else
- -- Find a new target if there is no current one
- currentTarget = UpdateTargetPlayers()
- end
- end)
- end
- local function Reinitialize()
- Disconnect() -- Ensure any previous connections are cleared
- currentTarget = UpdateTargetPlayers()
- BringTargetPlayerUntilHealthZero()
- end
- -- Initialize the targeting process
- Reinitialize()
- player.CharacterAdded:Connect(function(char)
- character = char
- localroot = character:WaitForChild("HumanoidRootPart")
- -- Reinitialize the targeting process
- Reinitialize()
- end)
- game.Players.PlayerAdded:Connect(Reinitialize)
- game.Players.PlayerRemoving:Connect(Reinitialize)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement