1 | |
2 | |
3 | local player = game.Players.LocalPlayer |
4 | local userInputService = game:GetService("UserInputService") |
5 | local runService = game:GetService("RunService") |
6 | |
7 | local toggled = false |
8 | |
9 | |
10 | local function findNearestTarget() |
11 | local nearestDistance = math.huge |
12 | local nearestTarget = nil |
13 | local playerPosition = player.Character and player.Character:FindFirstChild("HumanoidRootPart").Position |
14 | |
15 | if not playerPosition then return end |
16 | |
17 | for _, otherPlayer in ipairs(game.Players:GetPlayers()) do |
18 | if otherPlayer.Character and otherPlayer ~= player then |
19 | local humanoid = otherPlayer.Character:FindFirstChild("Humanoid") |
20 | local leftArm = otherPlayer.Character:FindFirstChild("Left Arm") or otherPlayer.Character:FindFirstChild("LeftUpperArm") |
21 | local rightArm = otherPlayer.Character:FindFirstChild("Right Arm") or otherPlayer.Character:FindFirstChild("RightUpperArm") |
22 | if humanoid and leftArm and rightArm and humanoid.Health > 0 then |
23 | local targetPosition = leftArm.Position |
24 | local distance = (targetPosition - playerPosition).magnitude |
25 | if distance < 16 and distance < nearestDistance then |
26 | |
27 | local hrp = player.Character and player.Character:FindFirstChild("HumanoidRootPart") |
28 | if hrp then |
29 | local hrpPos = hrp.Position |
30 | local yOffset = -0.1 |
31 | if targetPosition.Y >= hrpPos.Y + yOffset then |
32 | nearestDistance = distance |
33 | nearestTarget = otherPlayer |
34 | end |
35 | end |
36 | end |
37 | end |
38 | end |
39 | end |
40 | |
41 | return nearestTarget |
42 | end |
43 | |
44 | |
45 | local function moveToNearestTarget() |
46 | if not toggled then return end |
47 | |
48 | local target = findNearestTarget() |
49 | if target then |
50 | local leftArm = target.Character:FindFirstChild("Left Arm") or target.Character:FindFirstChild("LeftUpperArm") |
51 | local rightArm = target.Character:FindFirstChild("Right Arm") or target.Character:FindFirstChild("RightUpperArm") |
52 | local head = target.Character:FindFirstChild("Head") |
53 | if leftArm and rightArm and head then |
54 | local character = player.Character |
55 | local humanoid = character and character:FindFirstChild("Humanoid") |
56 | local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart") |
57 | if humanoid and humanoidRootPart then |
58 | |
59 | local directionToPlayer = (player.Character.HumanoidRootPart.Position - head.Position).unit |
60 | local targetLookDirection = head.CFrame.LookVector.unit |
61 | local dotProduct = directionToPlayer:Dot(targetLookDirection) |
62 | |
63 | |
64 | if dotProduct > 0.5 or (rightArm.Position - humanoidRootPart.Position).magnitude < 5 then |
65 | |
66 | local offsetDirection = (leftArm.Position - rightArm.Position).unit |
67 | local offsetPosition = leftArm.Position + offsetDirection * 3 |
68 | humanoid:MoveTo(offsetPosition) |
69 | |
70 | |
71 | local lookAtPosition = Vector3.new(leftArm.Position.X, humanoidRootPart.Position.Y, leftArm.Position.Z) |
72 | humanoidRootPart.CFrame = CFrame.new(humanoidRootPart.Position, lookAtPosition) * CFrame.Angles(0, math.rad(25), 0) |
73 | else |
74 | |
75 | humanoid:MoveTo(leftArm.Position) |
76 | end |
77 | end |
78 | end |
79 | end |
80 | end |
81 | |
82 | |
83 | local function toggleMovement(input) |
84 | if input.KeyCode == Enum.KeyCode.Q then |
85 | toggled = not toggled |
86 | end |
87 | end |
88 | |
89 | |
90 | userInputService.InputBegan:Connect(toggleMovement) |
91 | |
92 | |
93 | runService.RenderStepped:Connect(moveToNearestTarget) |