local Players = game:GetService("Players") local player = Players.LocalPlayer local CHASE_DISTANCE = 50 local WANDER_DISTANCE = 25 local WANDER_DELAY = 2 local function start(character) local humanoid = character:WaitForChild("Humanoid") local root = character:WaitForChild("HumanoidRootPart") while character.Parent and humanoid.Health > 0 do local target = nil local closestDistance = CHASE_DISTANCE -- Check for players within CHASE_DISTANCE for _, otherPlayer in Players:GetPlayers() do if otherPlayer ~= player and otherPlayer.Character then local otherRoot = otherPlayer.Character:FindFirstChild("HumanoidRootPart") local otherHumanoid = otherPlayer.Character:FindFirstChild("Humanoid") if otherRoot and otherHumanoid and otherHumanoid.Health > 0 then local distance = (root.Position - otherRoot.Position).Magnitude if distance <= CHASE_DISTANCE then if distance < closestDistance then closestDistance = distance target = otherRoot end end end end end -- PLAYER IS CLOSE → CHASE if target then humanoid:MoveTo(target.Position) -- NOBODY IS CLOSE → WANDER else local destination = root.Position + Vector3.new( math.random(-WANDER_DISTANCE, WANDER_DISTANCE), 0, math.random(-WANDER_DISTANCE, WANDER_DISTANCE) ) humanoid:MoveTo(destination) task.wait(WANDER_DELAY) end task.wait(0.1) end end if player.Character then start(player.Character) end player.CharacterAdded:Connect(start)